Count the number of rows and columns of a Pandas dataframe

You can try different methods to get the number of rows and columns of the dataframe:

  1. len(df)
  2. len(df.index)
  3. df.shape[0]
  4. df[df.columns[0]].count()
  5. df.count()
  6. df.size

First let's create a data frame with values.

import pandas as pd import numpy as np df = pd.DataFrame() df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben'] df['TotalMarks'] = [82, 38, 63,22,55,40] df['Grade'] = ['A', 'E', 'B','E','C','D'] df['Promoted'] = [True, False,True,False,True,True] df
Name TotalMarks Grade Promoted 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True 5 Ben 40 D True

Pandas DataFrame row count

Using len()

You can use len() method to find the number of rows in dataFrame.
row_count = len(df) # Return number of rows row_count
6

Or

row_count = len(df.index) # Return number of rows row_count
6

Using shape[0]

row_count = df.shape[0] row_count
6

Another method:

row_count = df[df.columns[0]].count() row_count
6

Get Pandas DataFrame column count

Using shape[1]

col_count = df.shape[1] col_count
4

Using len()

col_count = len(df.columns) col_count
4

Pandas DataFrame column with row count

count = df.count() count
Name 6 TotalMarks 6 Grade 6 Promoted 6 dtype: int64

Total number of rows and column in DataFrame

df.size
24