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

问题描述

我有下表:

注意:NSRCODE和PBL_AWI都是索引的

Note: Both NSRCODE and PBL_AWI are index's

注意:尚未填写的%of area"列.

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数据框中过滤多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:21