Python Pandas - 显示来自 DataFrame 的特定行数
要显示DataFrame中的特定行数,请使用该head()函数。将参数设置为要提取的行记录数。例如,对于10行,请提及-
dataFrame.head(10)
首先,让我们使用别名导入所需的库-
import pandas as pd
我们的CSV在桌面上,如下面的路径所示-
C:\\Users\\amit_\\Desktop\\CarRecords.csv
让我们阅读CSV文件并创建PandasDataFrame-
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")
返回特定的行数,即在这种情况下,我们将返回前5行记录-
dataFrame.head(5)
示例
以下是代码
import pandas as pd #读取csv文件 dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv") print("DataFrame...\n",dataFrame) #返回前5行记录 print("DataFrame 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 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