In this python pandas program, we will count the number of rows and columns in a DataFrame using pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Count the number of rows and columns in a DataFrame using df.shape().
- Print the output.
import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print(df)
print("No. of rows: ",df.shape[0])
print("No. of columns: ",df.shape[1])
Output :
0 Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
No. of rows: 4
No. of columns: 3