Count the number of rows and columns of a Pandas dataframe
Pandas allow us to get the shape of the Dataframe by counting the numbers of rows and columns in the Dataframe. You can try various approaches to know How to count the number of rows and columns in a Pandas.
Example:
Input: {'name': ['Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura'], 'score': [98, 80, 60, 85, 49, 92], 'age': [20, 25, 22, 24, 21, 20], 'qualify_label': ['yes', 'yes', 'no','yes', 'no', 'yes']} Output: Number of Rows: 6 Number of Columns: 4
Count the number of rows and columns of Dataframe using len(df.axes[]) function
Let’s take an example of a Dataframe that consists of data on exam results of students. To get the number of rows, and columns we can use len(df.axes[]) function in Python.
Python3
# importing pandas import pandas as pd result_data = { 'name' : [ 'Katherine' , 'James' , 'Emily' , 'Michael' , 'Matthew' , 'Laura' ], 'score' : [ 98 , 80 , 60 , 85 , 49 , 92 ], 'age' : [ 20 , 25 , 22 , 24 , 21 , 20 ], 'qualify_label' : [ 'yes' , 'yes' , 'no' , 'yes' , 'no' , 'yes' ]} # creating dataframe df = pd.DataFrame(result_data, index = None ) # computing number of rows rows = len (df.axes[ 0 ]) # computing number of columns cols = len (df.axes[ 1 ]) print (df) print ( "Number of Rows: " , rows) print ( "Number of Columns: " , cols) |
Output :

Count the number of rows and columns of Dataframe using info() function
Pandas dataframe.info() function is used to get a concise summary of the Dataframe. Here we can see that we get a summary detail of the Dataframe that contains the number of rows and columns.
Python3
# importing pandas import pandas as pd # creating dataframe df = pd.DataFrame({ 'name' : [ 'Katherine' , 'James' , 'Emily' , 'Michael' , 'Matthew' , 'Laura' ], 'score' : [ 98 , 80 , 60 , 85 , 49 , 92 ], 'age' : [ 20 , 25 , 22 , 24 , 21 , 20 ], 'qualify_label' : [ 'yes' , 'yes' , 'no' , 'yes' , 'no' , 'yes' ]}) print (df.info()) |
Output:
<class 'pandas.core.frame.DataFrame'> RangeIndex: 6 entries, 0 to 5 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 name 6 non-null object 1 score 6 non-null int64 2 age 6 non-null int64 3 qualify_label 6 non-null object dtypes: int64(2), object(2) memory usage: 320.0+ bytes None
Count the number of rows and columns of Dataframe using len() function.
The len() function returns the length rows of the Dataframe, we can filter a number of columns using the df.columns to get the count of columns.
Python3
# importing pandas import pandas as pd # creating dataframe df = pd.DataFrame({ 'name' : [ 'Katherine' , 'James' , 'Emily' , 'Michael' , 'Matthew' , 'Laura' ], 'score' : [ 98 , 80 , 60 , 85 , 49 , 92 ], 'age' : [ 20 , 25 , 22 , 24 , 21 , 20 ], 'qualify_label' : [ 'yes' , 'yes' , 'no' , 'yes' , 'no' , 'yes' ]}) print ( len (df)) print ( len (df.columns)) |
Output:
6 4
Count the number of rows and columns of Dataframe using shape.
Here, we will try a different approach for calculating rows and columns of a Dataframe of the imported CSV file, and counting the rows and columns using df.shape.
Python3
# importing pandas import pandas as pd # importing csv file df = pd.read_csv( print (df.head()) # obtaining the shape print ( "shape of dataframe" , df.shape) # obtaining the number of rows print ( "number of rows : " , df.shape[ 0 ]) # obtaining the number of columns print ( "number of columns : " , df.shape[ 1 ]) |
Output :


Count the number of rows and columns of Dataframe using the size
The size returns multiple rows and columns. i.e Here, the number of rows is 6, and the number of columns is 4 so the multiple rows and columns will be 6*4=24.
Python3
# importing pandas import pandas as pd # creating dataframe df = pd.DataFrame({ 'name' : [ 'Katherine' , 'James' , 'Emily' , 'Michael' , 'Matthew' , 'Laura' ], 'score' : [ 98 , 80 , 60 , 85 , 49 , 92 ], 'age' : [ 20 , 25 , 22 , 24 , 21 , 20 ], 'qualify_label' : [ 'yes' , 'yes' , 'no' , 'yes' , 'no' , 'yes' ]}) print (df.size) |
Output:
24
Count the number of rows of a Pandas Dataframe using count() and index.
Using count() and index we can get the number of rows present in the Dataframe.
Python3
# importing pandas import pandas as pd # creating dataframe df = pd.DataFrame({ 'name' : [ 'Katherine' , 'James' , 'Emily' , 'Michael' , 'Matthew' , 'Laura' ], 'score' : [ 98 , 80 , 60 , 85 , 49 , 92 ], 'age' : [ 20 , 25 , 22 , 24 , 21 , 20 ], 'qualify_label' : [ 'yes' , 'yes' , 'no' , 'yes' , 'no' , 'yes' ]}) print (df[df.columns[ 0 ]].count()) print ( len (df.index)) |
Output:
6 6
Please Login to comment...