Get the number of rows and number of columns in Pandas Dataframe
Pandas provide data analysts a variety of pre-defined functions to Get the number of rows and columns in a data frame. In this article, we will learn about the syntax and implementation of few such functions.
Method 1: Using df.axes() Method
axes() method in pandas allows to get the number of rows and columns in a go. It accepts the argument ‘0’ for rows and ‘1’ for columns.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Syntax: df.axes[0 or 1]
Parameters:
0: for number of Rows
1: for number of columns
Example:
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'], 'Age' : [23, 21, 22, 21], 'University' : ['BHU', 'JNU', 'DU', 'BHU'],} # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],\ index = ['a', 'b', 'c', 'd']) # Get the number of rows and columnsrows = len(df.axes[0])cols = len(df.axes[1]) # Print the number of rows and columnsprint("Number of Rows: " + str(rows))print("Number of Columns: " + str(cols)) |
Output:
Number of Rows: 4 Number of Columns: 3
Method 2: Using df.info() Method
df.info() method provides all the information about the data frame, including the number of rows and columns.
Syntax:
df.info
Example:
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'], 'Age' : [23, 21, 22, 21], 'University' : ['BHU', 'JNU', 'DU', 'BHU'],} # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'], index = ['a', 'b', 'c', 'd']) # Get the info of data framedf.info() |
Output:

Here in the above code, the value in the Index gives the number of rows and the value in Data columns gives the number of columns.
Method 3: Using len() Method
len() method is used to get the number of rows and number of columns individually.
Syntax:
len(df) and len(df.columns)
Example 1: Get the number of rows
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'], 'Age' : [23, 21, 22, 21], 'University' : ['BHU', 'JNU', 'DU', 'BHU'],} # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'], index = ['a', 'b', 'c', 'd']) # Get the number of rowsprint("Number of Rows:", len(df)) |
Output:
Number of Rows: 4
Example 2: Get the number of columns
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'], 'Age' : [23, 21, 22, 21], 'University' : ['BHU', 'JNU', 'DU', 'BHU'],} # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'], index = ['a', 'b', 'c', 'd']) # Get the number of columnsprint("Number of Columns:", len(df.columns)) |
Output:
Number of Columns: 3
Method 4: Using df.shape() Method
df.shape() method returns the number of rows and columns in the form of a tuple.
Example:
# import pandas libraryimport pandas as pd # dictionary with list object in valuesdetails = { 'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'], 'Age' : [23, 21, 22, 21], 'University' : ['BHU', 'JNU', 'DU', 'BHU'],} # creating a Dataframe object df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'], index = ['a', 'b', 'c', 'd']) # Get the number of Rows and columnsdf.shape |
Output:
(4, 3)

