To count the rows and columns in a DataFrame, use the shape property. At first, let’s say we have the a CSV file on the Desktop as shown in the below path −
C:\\Users\\amit_\\Desktop\\CarRecords.csv
Read the CSV file −
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")
Let us now count the rows and columns using shape
dataFrame.shape
Example
Following is the code −
import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv") print("DataFrame...\n",dataFrame) # count the rows and columns in a DataFrame print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape) # returns top 5 row records print("\nDataFrame with specific number of rows...\n",dataFrame.head(5))
Output
This will produce the following output −
DataFrame... Car Place UnitsSold 0 Audi Bangalore 80 1 Porsche Mumbai 110 2 RollsRoyce Pune 100 3 BMW Delhi 95 4 Mercedes Hyderabad 80 5 Lamborghini Chandigarh 80 6 Audi Mumbai 100 7 Mercedes Pune 120 8 Lamborghini Delhi 100 Number of rows and column in our DataFrame = (9, 3) DataFrame with specific number of rows ... Car Place UnitsSold 0 Audi Bangalore 80 1 Porsche Mumbai 110 2 RollsRoyce Pune 100 3 BMW Delhi 95 4 Mercedes Hyderabad 80