In this section we will learn how to get the number of rows and number of columns in pandas dataframe python.
- df.shape – will give the number of rows and columns of the dataframe in pandas.
- len(df.index)- will give the number of rows of the dataframe in pandas.
- len(df.columns) – will give the number of columns of the dataframe in pandas.
Create dataframe :
import pandas as pd import numpy as np #Create a DataFrame d = { 'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine', 'Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'], 'Exam':['Semester 1','Semester 1','Semester1','Semester1','Semester1','Semester 1', 'Semester 2','Semester 2','Semester2','Semester2','Semester2','Semester 2'], 'Subject':['Mathematics','Mathematics','Mathematics','Science','Science','Science', 'Mathematics','Mathematics','Mathematics','Science','Science','Science'], 'Score':[62,47,55,74,31,77,85,63,42,67,89,81]} df = pd.DataFrame(d,columns=['Name','Exam','Subject','Score']) print(df)
so the resultant dataframe will be
Get the number of rows and columns of the dataframe in pandas python:
df.shape
we can use dataframe.shape to get the number of rows and number of columns of a dataframe in pandas
So the result will be
(12, 4)
Get the number of rows of the dataframe in pandas python:
len(df.index)
so the result will be
12
Get the number of columns of the dataframe in pandas python:
len(df.columns)
so the result will be
4