Python - 使用 Pandas 在 DataFrame 中搜索特定值
我们可以在DataFrame中搜索特定值。使用iloc获取所需的值并显示整行。首先,导入所需的库-
import pandas as pd
创建一个具有4列的DataFrame-
dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],"Units_Sold": [ 100, 120, 150, 110, 200, 250]
})让我们搜索注册价格为500的汽车-
for i in range(len(dataFrame.Car)):
if 5000 == dataFrame.Reg_Price[i]:
indx = i现在,显示找到的值-
dataFrame.iloc[indx]
示例
以下是代码-
import pandas as pd
#创建数据框
dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],"Units_Sold": [ 100, 120, 150, 110, 200, 250]
})
print"DataFrame ...\n",dataFrame
#查车注册价500
for i in range(len(dataFrame.Car)):
if 5000 == dataFrame.Reg_Price[i]:
indx = i
#显示找到的值
print"\nSearched DataFrame for the following specific value...\n",dataFrame.iloc[indx]输出结果这将产生以下输出-
DataFrame ...
Car Cubic_Capacity Reg_Price Units_Sold
0 BMW 2000 7000 100
1 Lexus 1800 1500 120
2 Tesla 1500 5000 150
3 Mustang 2500 8000 110
4 Mercedes 2200 9000 200
5 Jaguar 3000 6000 250
Searched DataFrame for the following specific value...
Car Tesla
Cubic_Capacity 1500
Reg_Price 5000
Units_Sold 150
Name: 2, dtype: object热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短