Get the number of rows in a Pandas DataFrame

Python

Pandas

DataFrame

Get row count

Luc B.

Python

Pandas

DataFrame

When working with DataFrames, one often needs the number of rows to carry out certain operations.

Code Example

Call len on a DataFrame to obtain the number of rows.

import pandas as pd

# Create a DataFrame with 4 rows and 3 columns
penguins = pd.DataFrame({
    'name': ['Tim', 'Midge', 'Ronald', 'Amy'], 
    'favorite_food': ['fish', 'fish', 'fish', 'fingers'],
    'age': [2, 18, 12, 8],
})

# Get the number of rows here
print('Number of Rows:', len(penguins))
Number of Rows: 4

More Examples

Length of the Index

The above code is equivalent to calling len on the DataFrame index.

print('Number of Rows:', len(penguins.index))
Number of Rows: 4

DataFrame Shape

The rows count can also be obtained using the DataFrame's shape property.

rows, cols = penguins.shape
print('Number of Rows:', rows)
Number of Rows: 4