python

Get the count of rows and columns of a Pandas DataFrame

You can use DataFrame.shape to get the total number of rows and columns that exists in a Pandas DataFrame.

import pandas as pd

data = {
  "subjects": ["Math", "Physics", "Chemistry", "English", "Hindi"],
  "scores": [90, 80, 87, 80, 70]
}

df = pd.DataFrame(data)

# Get rows and columns count
result = df.shape

print("Rows: ", result[0])
print("Columns: ", result[1])

Output

Rows:  5
Columns:  2

DataFrame.shape returns a tuple that contains two integer type values. The first value is the count of Rows in the DataFrame and the second value is the total number of Columns in the DataFrame.

Was this helpful?