Python Pandas - 从数据框中选择行子集
pythonserver side programmingprogramming更新于 2025/11/19 18:20:17
要选择行子集,请使用条件并获取数据。
假设以下是在 Microsoft Excel 中打开的 CSV 文件的内容 −

首先,将数据从 CSV 文件加载到 Pandas DataFrame −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
假设我们想要"Units"大于 100 的汽车记录,即行子集。为此,使用 −
dataFrame[dataFrame["Units"] > 100]
现在,假设我们想要"Reg_Price"小于 100 的汽车记录,即行子集。为此,使用 −
dataFrame[dataFrame["Reg_Price"] < 3000]
示例
以下是代码 −
import pandas as pd
# 将 CSV 文件中的数据加载到 Pandas DataFrame 中
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
print("\nReading the CSV file...\n",dataFrame)
# 显示两列
res2 = dataFrame[['Reg_Price','Units']];
print("\nDisplaying two columns : \n",res2)
# 选择行子集
print("\nSelect cars with Units more than 100: \n",dataFrame[dataFrame["Units"] > 100])
# 选择行子集
print("\nSelect cars with Reg_Price less than 3000: \n",dataFrame[dataFrame["Reg_Price"] < 3000])
输出
这将产生以下输出 −
Reading the CSV file... Car Reg_Price Units 0 BMW 2500 100 1 Lexus 3500 80 2 Audi 2500 120 3 Jaguar 2000 70 4 Mustang 2500 110 Displaying only one column Car : Reg_Price Units 0 2500 100 1 3500 80 2 2500 120 3 2000 70 4 2500 110 Name: Car, dtype: object Select cars with Units more than 100: Car Reg_Price Units 2 Audi 2500 120 4 Mustang 2500 110 Select cars with Reg_Price less than 3000: Car Reg_Price Units 0 BMW 2500 100 2 Audi 2500 120 3 Jaguar 2000 70 4 Mustang 2500 110
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

