如何高效地沿多维切片Pandas Series的MultiIndex?
我在处理Pandas的时候,感觉自己像是在大海里迷路了,搞不清楚ix、xs、多重索引、get_level_values等等。
我有一个三层多重索引的Series。我想知道,有什么高效的方法可以根据不同层级的值来切片我的Series?
我的Series长这样:
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
到目前为止,我找到了一种解决方案,建议使用 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来做点什么,但前者似乎只能按特定值过滤,而后者只是根据Series中的位置进行索引?
1 个回答
4
这里有一个例子;这个例子需要当前的主版本,并将在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
,因为它们在Series中基本上是等价的;在索引DataFrame时更有用)
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