python - pandas remove observations depending on multi-index level value -
i have multi-index data frame levels 'id' , 'year':
value id year 10 2001 100 2002 200 11 2001 110 12 2001 200 2002 300 13 2002 210
i want keep id
s have values both years 2001 , 2002. means want obtain:
value id year 10 2001 100 2002 200 12 2001 200 2002 300
i know df.loc[df.index.get_level_values('year') == 2002]
works cannot extend account both 2001 , 2002. in advance.
how use groupby
, filter
:
df.groupby(level=0).filter( lambda df:np.in1d([2001, 2002], df.index.get_level_values(1)).all() )
Comments
Post a Comment