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?