【问题标题】:What is an efficient way to slice a Pandas Series with a MultiIndex along multiple dimensions?沿多个维度对带有 MultiIndex 的 Pandas 系列进行切片的有效方法是什么?
【发布时间】:2014-03-20 21:34:57
【问题描述】:

我迷失在 ix、xs、MultiIndex、get_level_values 和其他 Pandas 的海洋中。

我有一个具有 3 级多索引的系列。根据不同级别的值对我的系列进行切片的有效方法是什么?

我的系列看起来像这样:

days  id                      start_date
0     S0036-4665(00)04200108  2013-05-18      1
3     S0036-4665(00)04200108  2013-05-18      1
5     S0036-4665(00)04200108  2013-05-18      3
13    S0036-4665(00)04200108  2013-05-18      1
19    S0036-4665(00)04200108  2013-05-18      1
39    S0036-4665(00)04200108  2013-05-18      1
...

很明显,id 和 start_date 的值会随着你的声名鹊起而变化

我希望能够根据以下内容进行切片: - 数字范围内的天数 - 特定集合内的 id - start_date 在特定日期范围内

到目前为止,我找到了this solution,它建议使用df[df.index.get_level_values('a').isin([5, 7, 10, 13])],我发现我可以这样做:

s.select(lambda x: x[0] < 20 and (x[1] in set('some id', 'other id') ))

其中任何一个都是最好的解决方案吗?我觉得应该可以用xs,或者ix来做点什么,但是前者好像只让你按特定值过滤,而后者只对序列中的位置进行索引?

【问题讨论】:

标签: python pandas


【解决方案1】:

这是一个例子;这需要当前的 master 并且将在 0.14 中可用。 文档在这里:http://pandas-docs.github.io/pandas-docs-travis/indexing.html#multiindexing-using-slicers

创建一个多索引(这恰好是输入的笛卡尔积,但是 没必要)

In [28]: s = Series(np.arange(27),
               index=MultiIndex.from_product(
                     [[1,2,3],
                      ['foo','bar','bah'],
                      date_range('20130101',periods=3)])
                    ).sortlevel()

始终确保您已完全排序

In [29]: s.index.lexsort_depth
Out[29]: 3

In [30]: s
Out[30]: 
1  bah  2013-01-01     6
        2013-01-02     7
        2013-01-03     8
   bar  2013-01-01     3
        2013-01-02     4
        2013-01-03     5
   foo  2013-01-01     0
        2013-01-02     1
        2013-01-03     2
2  bah  2013-01-01    15
        2013-01-02    16
        2013-01-03    17
   bar  2013-01-01    12
        2013-01-02    13
        2013-01-03    14
   foo  2013-01-01     9
        2013-01-02    10
        2013-01-03    11
3  bah  2013-01-01    24
        2013-01-02    25
        2013-01-03    26
   bar  2013-01-01    21
        2013-01-02    22
        2013-01-03    23
   foo  2013-01-01    18
        2013-01-02    19
        2013-01-03    20
dtype: int64

这有助于减少冗长的定义(这将级别组合在一起以获得单个 轴)

In [33]: idx = pd.IndexSlice

选择我,其中级别 0 是 2,级别 1 是 bar 或 foo

In [31]: s.loc[idx[[2],['bar','foo']]]
Out[31]: 
2  bar  2013-01-01    12
        2013-01-02    13
        2013-01-03    14
   foo  2013-01-01     9
        2013-01-02    10
        2013-01-03    11
dtype: int64

同上,但2级也等于20130102

In [32]: s.loc[idx[[2,3],['bar','foo'],'20130102']]
Out[32]: 
2  bar  2013-01-02    13
   foo  2013-01-02    10
3  bar  2013-01-02    22
   foo  2013-01-02    19
dtype: int64

这是一个使用布尔索引器而不是级别索引器的示例。

In [43]: s.loc[idx[[2,3],['bar','foo'],s<20]]
Out[43]: 
2  bar  2013-01-01    12
        2013-01-02    13
        2013-01-03    14
   foo  2013-01-01     9
        2013-01-02    10
        2013-01-03    11
3  foo  2013-01-01    18
        2013-01-02    19
dtype: int64

这是一个省略某些级别的示例(请注意,这里没有使用idx,因为它们本质上与系列等效;在索引数据帧时更有用)

In [47]: s.loc[:,['bar','foo'],'20130102']
Out[47]: 
1  bar  2013-01-02     4
   foo  2013-01-02     1
2  bar  2013-01-02    13
   foo  2013-01-02    10
3  bar  2013-01-02    22
   foo  2013-01-02    19
dtype: int64

【讨论】:

  • 谢谢。我还没有机会测试这个,因为我还没有从 Master 更新熊猫。但它似乎确实满足了我的需求。
  • @Jeff,你没有“误用”IndexSlice 吗?因为我认为这是为列或索引获取不同级别的切片。但不适用于不同级别之间的一个轴(列/索引)内(我的意思是:一个轴的所有切片/选择都应该在一个 idx 内)。在任何情况下,您都可以在示例中省略idx,例如s.loc[[2],['bar','foo']],或者将所有级别都放在idx中,例如s.loc[idx[[2],['bar','foo']]],以防止在使用数据框时第一个不再起作用的惊喜一个系列。
  • @joris 是的。我修好了;在索引系列时使用pd.IndexSlice 不是绝对必要的,而是一个有用的习惯用法
猜你喜欢
  • 1970-01-01
  • 2014-12-17
  • 2014-05-28
  • 2021-07-05
  • 2018-10-21
  • 1970-01-01
  • 2011-12-20
  • 2012-12-11
  • 1970-01-01
相关资源
最近更新 更多