Python无法基于同一数据帧中的多个列值查找列值

Python无法基于同一数据帧中的多个列值查找列值,python,pandas,Python,Pandas,df: 现在我想得到城市名称,其中“水果=橘子,价格=5” 不工作,错误如下: df.loc[(df['fruit'] == 'orange') & (df['price'] == 5) , 'city'].iloc[0] 使用的版本:Python 3.5您可以逐步创建掩码,并查看它们的外观: IndexError: single positional indexer is out-of-bounds 看起来您的查询返回了一个空结果。我能够找到单个条件为df.loc[(df['Fru

df:

现在我想得到城市名称,其中“水果=橘子,价格=5”

不工作,错误如下:

df.loc[(df['fruit'] == 'orange') & (df['price'] == 5) , 'city'].iloc[0]

使用的版本:Python 3.5

您可以逐步创建掩码,并查看它们的外观:

IndexError: single positional indexer is out-of-bounds

看起来您的查询返回了一个空结果。我能够找到单个条件为df.loc[(df['Fruit']=='orange')].iloc[0]的结果。但在使用多个条件时会出现错误。请阅读我在基本打字问题上的评论
df.loc[(df['fruit']='orange')和(df['price']==5),'city']。iloc[0]
@Wen:很抱歉,我在发布问题时出现了打字错误。仍然会出现错误,因为:indexer:single positional indexer超出了范围
IndexError: single positional indexer is out-of-bounds
import pandas as pd

df = pd.DataFrame([{'city': 'Pune', 'fruit': 'apple', 'no': 1L, 'price': 10L},
 {'city': 'Mumbai', 'fruit': 'apple', 'no': 2L, 'price': 20L},
 {'city': 'Nagpur', 'fruit': 'orange', 'no': 3L, 'price': 5L},
 {'city': 'Delhi', 'fruit': 'orange', 'no': 4L, 'price': 7L},
 {'city': 'Bangalore', 'fruit': 'Mango', 'no': 5L, 'price': 20L},
 {'city': 'Chennai', 'fruit': 'Mango', 'no': 6L, 'price': 15L}])

m1 = df['fruit'] == 'orange'
m2 = df['price'] == 5

df[m1&m2]['city'].values[0]  # 'Nagpur'