Get the number of rows and number of columns in Pandas Dataframe - GeeksforGeeks

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.
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 Rows1: 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

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.
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

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)
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()