使用复合(分层)索引从Pandas数据框中选择行 [英] Selecting rows from a Pandas dataframe with a compound (hierarchical) index

查看:678
本文介绍了使用复合(分层)索引从Pandas数据框中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这是微不足道的,但我还没发现可以让我根据分层键的值从Pandas dataframe 中选择行的咒语。因此,例如,假设我们有以下数据框

I'm suspicious that this is trivial, but I yet to discover the incantation that will let me select rows from a Pandas dataframe based on the values of a hierarchical key. So, for example, imagine we have the following dataframe:

import pandas
df = pandas.DataFrame({'group1': ['a','a','a','b','b','b'],
                       'group2': ['c','c','d','d','d','e'],
                       'value1': [1.1,2,3,4,5,6],
                       'value2': [7.1,8,9,10,11,12]
})
df = df.set_index(['group1', 'group2'])

df看起来像我们预期的那样:

df looks as we would expect:

如果df未在group1上编入索引,我可以执行以下操作:

If df were not indexed on group1 I could do the following:

df['group1' == 'a']

但是这个带有索引的数据帧失败了。所以也许我应该把它想象成一个带有等级索引的Pandas系列:

But that fails on this dataframe with an index. So maybe I should think of this like a Pandas series with a hierarchical index:

df['a','c']

不。那也失败了。

那么如何选出以下所有行:

So how do I select out all the rows where:


  1. group1 = ='a'

  2. group1 =='a'& group2 =='c'

  3. group2 =='c'

  4. group1 in ['a','b','c']

  1. group1 == 'a'
  2. group1 == 'a' & group2 == 'c'
  3. group2 == 'c'
  4. group1 in ['a','b','c']


推荐答案

尝试使用 xs 非常精确:

In [5]: df.xs('a', level=0)
Out[5]: 
        value1  value2
group2                
c          1.1     7.1
c          2.0     8.0
d          3.0     9.0

In [6]: df.xs('c', level='group2')
Out[6]: 
        value1  value2
group1                
a          1.1     7.1
a          2.0     8.0

这篇关于使用复合(分层)索引从Pandas数据框中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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