具有MultiIndex的pandas DataFrame中的条件选择行

时间:2018-12-21 15:57:02

标签: python pandas multi-index

我有一个这样的DataFrame:

df = pd.DataFrame(np.random.randn(6, 6),
                  columns=pd.MultiIndex.from_arrays((['A','A','A','B','B','B'], 
                                                     ['a', 'b', 'c', 'a', 'b', 'c'])))
df
          A                             B                    
          a         b         c         a         b         c
0 -0.089902 -2.235642  0.282761  0.725579  1.266029 -0.354892
1 -1.753303  1.092057  0.484323  1.789094 -0.316307  0.416002
2 -0.409028 -0.920366 -0.396802 -0.569926 -0.538649 -0.844967
3  1.789569 -0.935632  0.004476 -1.873532 -1.136138 -0.867943
4  0.244112  0.298361 -1.607257 -0.181820  0.577446  0.556841
5  0.903908 -1.379358  0.361620  1.290646 -0.523404 -0.518992

我只想选择c列中值大于0的行。我认为必须使用pd.IndexSlice仅选择第二级索引c

idx = pd.IndexSlice
df.loc[:,idx[:,['c']]] > 0
       A      B
       c      c
0   True  False
1   True   True
2  False  False
3   True  False
4  False   True
5   True  False

所以,现在我希望我可以简单地做df[df.loc[:,idx[:,['c']]] > 0],但这给了我意外的结果:

df[df.loc[:,idx[:,['c']]] > 0]
    A                 B              
    a   b         c   a   b         c
0 NaN NaN  0.282761 NaN NaN       NaN
1 NaN NaN  0.484323 NaN NaN  0.416002
2 NaN NaN       NaN NaN NaN       NaN
3 NaN NaN  0.004476 NaN NaN       NaN
4 NaN NaN       NaN NaN NaN  0.556841
5 NaN NaN  0.361620 NaN NaN       NaN

我希望拥有的是所有值(不是NaN),并且只有任何c列都大于0的行。

          A                             B                    
          a         b         c         a         b         c
0 -0.089902 -2.235642  0.282761  0.725579  1.266029 -0.354892
1 -1.753303  1.092057  0.484323  1.789094 -0.316307  0.416002
3  1.789569 -0.935632  0.004476 -1.873532 -1.136138 -0.867943
4  0.244112  0.298361 -1.607257 -0.181820  0.577446  0.556841
5  0.903908 -1.379358  0.361620  1.290646 -0.523404 -0.518992

因此,我可能需要在其中的某个地方溜走any(),但是,我不确定该怎么做。有提示吗?

2 个答案:

答案 0 :(得分:4)

使用get_level_values

的另一个版本

df[(df.iloc[:, df.columns.get_level_values(1) == 'c'] > 0).any(axis=1)]

答案 1 :(得分:1)

您正在寻找any

df[(df.loc[:,idx[:,['c']]]>0).any(axis = 1)]
Out[133]: 
          A                             B                    
          a         b         c         a         b         c
1 -0.423313  0.459464 -1.457655 -0.559667 -0.056230  1.338850
3 -0.072396  1.305868 -1.239441 -0.708834  0.348704  0.260532
4 -1.415575  1.229508  0.148254 -0.812806  1.379552 -1.195062
5 -0.336973 -0.469335  1.345719  0.847943  1.465100 -0.285792