Pandas groupby and filter

I have a dataframe:

df = pd.DataFrame({'ID':[1,1,2,2,3,3], 
                   'YEAR' : [2011,2012,2012,2013,2013,2014], 
                   'V': [0,1,1,0,1,0],
                   'C':[00,11,22,33,44,55]})

I would like to group by ID and select a row with V = 0 in each group.

This does not work:

print(df.groupby(['ID']).filter(lambda x: x['V'] == 0)) 

Received error:

TypeError: filter function returned a series, but expected a scalar bool

How can I use a filter to achieve a goal? Thank.

EDIT : The condition for V can vary for each group, for example, for identifier 1, V == 1 for ID 2, it can be V == 0, and this information can be accessed through another DF:

df = pd.DataFrame({'ID':[1,2,3], 
                   'V': [0,1,0])

So how do you filter the rows within each group?

+4
source share
1 answer

, groupby , boolean indexing, , V 0:

print (df[df.V == 0])
    C  ID  V  YEAR
0   0   1  0  2011
3  33   2  0  2013
5  55   3  0  2014

, V 0 any, True False :

print(df.groupby(['ID']).filter(lambda x: (x['V'] == 0).any())) 
    C  ID  V  YEAR
0   0   1  0  2011
1  11   1  1  2012
2  22   2  1  2012
3  33   2  0  2013
4  44   3  1  2013
5  55   3  0  2014

- groupby - 2012 , no V==0:

print(df.groupby(['YEAR']).filter(lambda x: (x['V'] == 0).any())) 
    C  ID  V  YEAR
0   0   1  0  2011
3  33   2  0  2013
4  44   3  1  2013
5  55   3  0  2014
+5

Source: https://habr.com/ru/post/1666090/


All Articles