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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -