在多索引Python Panda数据帧中过滤多个项目 [英] Filtering muliple items in a multi-index Python Panda dataframe

查看:334
本文介绍了在多索引Python Panda数据帧中过滤多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

注意:NSRCODE和PBL_AWI都是索引

Note: Both NSRCODE and PBL_AWI are index's

注意:尚未填写的区域列的百分比。

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

NSRCODE  PBL_AWI          Area           % Of Area
CM       BONS             44705.492941
         BTNN            253854.591990
         FONG             41625.590370
         FONS             16814.159680
         Lake             57124.819333
         River             1603.906642
         SONS            583958.444751
         STNN             45603.837177
         clearcut        106139.013930
         disturbed       127719.865675
         lowland         118795.578059
         upland         2701289.270193
LBH      BFNN            289207.169650
         BONS           9140084.716743
         BTNI             33713.160390
         BTNN          19748004.789040
         FONG           1687122.469691
         FONS           5169959.591270
         FTNI            317251.976160
         FTNN           6536472.869395
         Lake            258046.508310
         River            44262.807900
         SONS           4379097.677405
         burn regen      744773.210860
         clearcut         54066.756790
         disturbed       597561.471686
         lowland       12591619.141842
         upland        23843453.638117

我如何过滤掉PBL_AWI索引中的项目?
例如我想保留['Lake','River','Upland']

How do I filter out item in the "PBL_AWI" index? For example I want to keep ['Lake', 'River', 'Upland']

推荐答案

你可以 get_level_values 与布尔切片结合使用。

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

同样的想法可以用许多不同的方式表达,例如 df [df.index.get_level_values('PBL_AWI')。isin(['Lake','River','Upland' ])

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

请注意,您的数据中有'upland'而不是'Upland'

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

这篇关于在多索引Python Panda数据帧中过滤多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆