Open In App

Count number of rows and columns in Pandas dataframe

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Pandas understanding number of rows and columns in a DataFrame is important for knowing structure of our dataset. Whether we’re cleaning the data, performing calculations or visualizing results finding shape of the DataFrame is one of the initial steps. In this article, we’ll explore various ways to count number of rows and columns in a Pandas DataFrame.

1. Using the shape Property

Commonly used method to count rows and columns is by using the df.shape property. The .shape property returns a tuple containing two values:

  • First value is number of rows.
  • Second value is number of columns.
Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
rows, columns = df.shape
print(f"Rows: {rows}, Columns: {columns}")

Output:

Rows: 3, Columns: 3

In above example, df.shape returns (3, 3) which means it has 3 rows and 3 columns. This method is quick and efficient for getting size of DataFrame at once.

2. Using the len() Function

The len() function can be applied to either index(rows) or columns of a DataFrame to get the respective counts. It is an repeative approach when you want to count rows or columns individually.

Example for Rows:

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
num_rows = len(df.index)
print(f"Number of rows: {num_rows}")

Output:

Number of rows: 3

Example for Columns:

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
num_columns = len(df.columns)
print(f"Number of columns: {num_columns}")

Output:

Number of columns: 3

3. Using the axes Attribute

The axes property provides access to both row and column labels as separate lists. We can use it to count rows or columns.

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
num_rows = len(df.axes[0]) 
num_columns = len(df.axes[1]) 
print(f"Rows: {num_rows}, Columns: {num_columns}")

Output:

Rows: 3, Columns: 3

4. Using the count() Method

The count() method counts non-NA/null values for each column (or row if specified). While this method doesn’t directly give us total count of rows or columns it can be useful when dealing with incomplete datasets containing missing values.

Example for Columns Count:

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
columns_count = df.count()
print(columns_count)

Output:

column-1

Using the count()

Example for Rows Count (Non-null values):

Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
rows_count = df.count(axis='columns')
print(rows_count)

Output:

COLUMN-2

Non-null values

All are different ways to count number of rows and column but in different scenarios.

  1. shape Property: Best for getting both row and column counts simultaneously.
  2. len() Function: Ideal for counting either rows or columns individually.
  3. axes Attribute: Provides flexibility but less commonly used.
  4. count() Method: Useful for counting non-null values in rows or columns.

By mastering these methods we can effectively analyze structure of our dataset and proceed with your data analysis tasks.



Next Article
Practice Tags :

Similar Reads