Get the number of rows and number of columns in Pandas Dataframe
Last Updated :
02 Jul, 2020
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.
Syntax: df.axes[0 or 1]
Parameters:
0: for number of Rows
1: for number of columns
Example:
Python3 1==
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'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 columns
rows = len(df.axes[0])
cols = len(df.axes[1])
# Print the number of rows and columns
print("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:
Python3 1==
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'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 frame
df.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
Python3 1==
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'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
print("Number of Rows:", len(df))
Output:
Number of Rows: 4
Example 2: Get the number of columns
Python3 1==
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'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 columns
print("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:
Python3 1==
# import pandas library
import pandas as pd
# dictionary with list object in values
details = {
'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 columns
df.shape
Output:
(4, 3)
Similar Reads
Count the number of rows and columns of Pandas dataframe In this article, we'll see how we can get the count of the total number of rows and columns in a Pandas DataFrame. There are different methods by which we can do this. Let's see all these methods with the help of examples. Example 1: We can use the dataframe.shape to get the count of rows and column
2 min read
Get number of rows and columns of PySpark dataframe In this article, we will discuss how to get the number of rows and the number of columns of a PySpark dataframe. For finding the number of rows and number of columns we will use count() and columns() with len() function respectively. df.count(): This function is used to extract number of rows from t
6 min read
Count number of rows and columns in Pandas dataframe In Pandas understanding number of rows and columns in a DataFrame is important for knowing structure of our dataset. Whether we're cleaning the data, performing calculations or visualizing results finding shape of the DataFrame is one of the initial steps. In this article, we'll explore various ways
3 min read
Count number of columns of a Pandas DataFrame Let's discuss how to count the number of columns of a Pandas DataFrame. Lets first make a dataframe. Example: Python3 # Import Required Libraries import pandas as pd import numpy as np # Create a dictionary for the dataframe dict = {'Name': ['Sukritin', 'Sumit Tyagi', 'Akriti Goel', 'Sanskriti', 'Ab
2 min read
Get the datatypes of columns of a Pandas DataFrame Let us see how to get the datatypes of columns in a Pandas DataFrame. TO get the datatypes, we will be using the dtype() and the type() function.Example 1 :Â Â python # importing the module import pandas as pd # creating a DataFrame dictionary = {'Names':['Simon', 'Josh', 'Amen', 'Habby', 'Jonathan',
2 min read
How to get column and row names in DataFrame? While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations. Note: For downloading the nba dataset used in the below examples Click Here Getting row names in Pandas dataframe First, let's
3 min read