Suppose I have the following dataframe:
df = pd.DataFrame({'color':['red', 'green', 'blue'], 'brand':['Ford','fiat', 'opel'], 'year':[2016,2016,2017]})
brand color year
0 Ford red 2016
1 fiat green 2016
2 opel blue 2017
I know that to select using multiple columns I can do something like:
new_df = df[(df['color']=='red')&(df['year']==2016)]
Now what I would like to do is find a way to use a dictionary to select the rows I want where the keys of the dictionary represent the columns mapping to the allowed values. For example applying the following dictionary {'color':'red', 'year':2016}
on df would yield the same result as new_df.
I can already do it with a for loop, but I'd like to know if there are any faster and/or more 'pythonic' ways of doing it!
Please include time taken of method.