Skip to content
Advertisement

Python Pandas finding column value based on multiple column values in same data frame

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

Now I want to get city name where “fruit= orange and price =5”

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

is not working and giving error as:

IndexError: single positional indexer is out-of-bounds

Versions used: Python 3.5

Advertisement

Answer

You could create masks step-wise and see how they look like:

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'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement