Python Pandas根据同一数据框中的多个列值查找列值

时间:2018-05-01 03:49:16

标签: python pandas

DF:

no  fruit    price  city
1    apple   10     Pune
2    apple   20     Mumbai
3    orange  5      Nagpur
4    orange  7      Delhi
5    Mango   20     Bangalore
6    Mango   15     Chennai

现在我希望获得城市名称“fruit = orange and price = 5”

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

无法正常工作并提供错误:

IndexError: single positional indexer is out-of-bounds

使用的版本:Python 3.5

2 个答案:

答案 0 :(得分:0)

您可以逐步创建蒙版,看看它们的样子:

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'

答案 1 :(得分:0)

可扩展和可编程的解决方案 - 利用多索引 Advanced indexing with hierarchical index

<块引用>
  1. 变量

    search_columns=['水果','价格'] search_values=['橙色','5'] target_column='city'

  2. 制作 df 的搜索列索引

    df_temp=df.set_index(search_columns)

  3. 使用'loc'方法获取值

    value=df_temp.loc[tuple(search_values),target_column]

结果是 <=2 个搜索列的标量或 pd.Series
分别用于 >2 个搜索列

<块引用>

片段:

import pandas as pd
columns = "fruit    price  city".split()
data = zip(
    'apple  apple   orange  orange  Mango   Mango'.split(),
    '10 20  5   7   20  15'.split(),
    'Pune   Mumbai  Nagpur  Delhi   Bangalore   Chennai'.split()
)
df = pd.DataFrame(data=data, columns=columns)
search_columns = ['fruit', 'price']
search_values = ['orange', '5']
target_column = 'city'
df_temp = df.set_index(search_columns)
value = df_temp.loc[tuple(search_values), target_column]
print(value)
<块引用>

结果:那格浦尔