DEV Community

Avnish
Avnish

Posted on

Pandas Count Rows – How to Get the Number of Rows in a Dataframe

Let's break down the ways to get the number of rows in a Pandas DataFrame with detailed explanations and examples.

1. Using the len() Function

The len() function in Python returns the number of items in an object. When you apply len() to a Pandas DataFrame, it returns the number of rows in the DataFrame.

Example:

import pandas as pd

data = {
    "name": ["John", "Jane", "Jade"],
    "age": [2, 10, 3]
}

df = pd.DataFrame(data)

num_of_rows = len(df)
print(f"The number of rows is {num_of_rows}")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • We import the Pandas library.
  • Create a dictionary named data with some data.
  • Convert this dictionary into a DataFrame df.
  • Use len(df) to get the number of rows in df.
  • Print the result.

Output:

The number of rows is 3
Enter fullscreen mode Exit fullscreen mode

2. Using the shape Attribute

The shape attribute of a DataFrame returns a tuple representing the dimensions of the DataFrame. The first element of the tuple is the number of rows, and the second is the number of columns.

Example:

num_of_rows = df.shape[0]
print(f"The number of rows is {num_of_rows}")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • df.shape returns a tuple, where the first element ([0]) is the number of rows.
  • Print the number of rows.

Output:

The number of rows is 3
Enter fullscreen mode Exit fullscreen mode

3. Using the index Attribute

The index attribute provides access to the index (row labels) of the DataFrame. You can use the size property of the index or pass the index to the len() function to get the number of rows.

Example using index.size:

num_of_rows = df.index.size
print(f"The number of rows is {num_of_rows}")
Enter fullscreen mode Exit fullscreen mode

Example using len(df.index):

num_of_rows = len(df.index)
print(f"The number of rows is {num_of_rows}")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • df.index.size and len(df.index) both give the number of rows in df.
  • Print the number of rows.

Output:

The number of rows is 3
Enter fullscreen mode Exit fullscreen mode

4. Using the axes Attribute

The axes attribute returns a list representing the axes of the DataFrame. The first element of the list is an index representing the rows. Similar to using the index attribute, you can get the size or use len() on the first element of axes.

Example using axes[0].size:

num_of_rows = df.axes[0].size
print(f"The number of rows is {num_of_rows}")
Enter fullscreen mode Exit fullscreen mode

Example using len(df.axes[0]):

num_of_rows = len(df.axes[0])
print(f"The number of rows is {num_of_rows}")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Both df.axes[0].size and len(df.axes[0]) return the number of rows in df.
  • Print the number of rows.

Output:

The number of rows is 3
Enter fullscreen mode Exit fullscreen mode

Additional: Counting Non-NaN Values in a Column

In cases where you might be interested in the count of non-missing (non-NaN) values in a specific column, you can use the .count() method on a DataFrame column.

Example:

non_nan_rows = df['age'].count()
print(f"The number of non-NaN rows in the 'age' column is {non_nan_rows}")
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • df['age'].count() counts the number of non-NaN entries in the 'age' column.
  • Print the result.

Output:

The number of non-NaN rows in the 'age' column is 3
Enter fullscreen mode Exit fullscreen mode

These examples cover different methods to get the number of rows in a Pandas DataFrame, offering flexibility depending on your specific needs or preferences.

Top comments (0)