Pandas : Count the number of rows and columns

Write a Pandas program to count the number of rows and columns of a given Data Frame with Practical Example.

Program Logic:

  • Create Dictionary say ‘marks’ which store marks of 5 subject.
  • Create Data Frame say ‘result’ which store the marks of 5 students using DataFrame method.
  • Display Data frame ‘result’ using print function.
  • Find total number of rows in given data frame using len method and pass result.axes[0] as an argument to len method
  • Store it into another variable total_rows
  • Find total number of rows in given data frame using len method and pass result.axes[1] as an argument to len method
  • Store it into another variable total_cols
  • Convert calculated data into string using str function.
  • Display output using print function
  • Exit.

Below is implementation code /Source code

Here is code to count the number of rows and columns of data frame.

import pandas as pd
marks  = { "English" :[67,89,90,55],
           "Maths":[55,67,45,56],
            "IP":[66,78,89,90],
           "Chemistry" :[45,56,67,65],
           "Biology":[54,65,76,87]}
result = pd.DataFrame(marks,index=["Athang","Sujata","Sushil","Sumedh"])
print("******************Marksheet****************")
print(result)
total_rows=len(result.axes[0])
total_cols=len(result.axes[1])
print("Number of Rows: "+str(total_rows))
print("Number of Columns: "+str(total_cols))

Output:

********Marksheet****************
English Maths IP Chemistry Biology
Athang 67 55 66 45 54
Sujata 89 67 78 56 65
Sushil 90 45 89 67 76
Sumedh 55 56 90 65 87
Number of Rows: 4
Number of Columns: 5

Below is snapshot of executable source code with output

pandas count number of rows and columns of data frame

Check Another Pandas program with Practical Example

Pandas program to find and replace the missing values in given Data Frame which do not have any valuable information

Pandas program to select the rows where score is missing

<