df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
produces
a b
0 1 4
1 2 5
2 3 6
Given a dict
d = {'a': 2, 'b': 5}
how would I extract the rows of the dataframe where the dict's keys values match all the column values -- so in this case
a b
1 2 5
You can compare with Series
and filter:
df[(df == pd.Series(d)).all(1)]
a b
1 2 5
This comparison is aligned on the index/columns and broadcasted for each row.
Compare the values and use indexing,
df[ (df.values == np.array(list(d.values()))).all(1) ]
a b
1 2 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With