Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Pandas DataFrame for elements in list [duplicate]

Tags:

python

pandas

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... .

like image 836
MichaelA Avatar asked Sep 16 '15 21:09

MichaelA


1 Answers

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
like image 124
EdChum Avatar answered Oct 14 '22 04:10

EdChum