I have 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 the row with V = 0 within each group.
This doesn't seem to work:
print(df.groupby(['ID']).filter(lambda x: x['V'] == 0))
Got an error:
TypeError: filter function returned a Series, but expected a scalar bool
How can I use filter to achieve the goal? Thank you.
EDIT: The condition on V may vary for each group, e.g., it could be V==0 for ID 1, V==1 for ID 2, and this info can be available through another DF:
df = pd.DataFrame({'ID':[1,2,3],
'V': [0,1,0])
So how to do row filtering within each group?