Python Pandas – 统计 DataFrame 中的行和列
如需统计 DataFrame 中的行和列,请使用 shape 属性。首先,我们假设桌面有一个 CSV 文件,路径如下所示:
C:\Users\amit_\Desktop\CarRecords.csv
读取 CSV 文件:
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")
现在,我们使用 shape 统计行和列
dataFrame.shape
示例
以下为代码:
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))
输出
这会生成以下输出:
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
广告