Filtering multiple items in a multi-index Python Panda dataframe

后端 未结 4 2017
轮回少年
轮回少年 2020-12-13 17:29

I have the following table:

Note: Both NSRCODE and PBL_AWI are index\'s

Note: the % Of area column would be filled out just have not done so yet.

<         


        
4条回答
  •  Happy的楠姐
    2020-12-13 17:44

    You can get_level_values in conjunction with Boolean slicing.

    In [50]:
    
    print df[np.in1d(df.index.get_level_values(1), ['Lake', 'River', 'Upland'])]
                              Area
    NSRCODE PBL_AWI               
    CM      Lake      57124.819333
            River      1603.906642
    LBH     Lake     258046.508310
            River     44262.807900
    

    The same idea can be expressed in many different ways, such as df[df.index.get_level_values('PBL_AWI').isin(['Lake', 'River', 'Upland'])]

    Note that you have 'upland' in your data instead of 'Upland'

提交回复
热议问题