I have a pandas DataFrame that contains values and additional information. I'd like to be able to extract values that only belong to one sort of information. I don't know upfront which and how many values will be queried. So it would be possible that one time only values with the additional information "foo" will be called, sometimes with the additional information 'bar' and 'baz', so with a simplified DataFrame
import pandas as pd
df = pd.DataFrame(
[[1, 'foo'], [2, 'bar'], [3, 'baz']], columns=['value', 'id'])
I tried
result = df[df.id in ['foo', 'bar']]
But I just get a ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). But I can't geht the any()-Function to give me results... .
Use isin
to test membership of a column against the passed in list:
In [30]:
df[df['id'].isin(['foo','bar'])]
Out[30]:
value id
0 1 foo
1 2 bar
Here isin
generates a boolean mask, we use this to filter the df:
In [31]:
df['id'].isin(['foo','bar'])
Out[31]:
0 True
1 True
2 False
Name: id, dtype: bool
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