Pandas 多重索引的层级名称切片
最新版本的Pandas支持多重索引切片。不过,要正确使用这些功能,我们需要知道不同层级的整数位置。
比如,下面的代码:
idx = pd.IndexSlice
dfmi.loc[idx[:,:,['C1','C3']],idx[:,'foo']]
假设我们知道第三行层级是我们想用来索引的,使用的是C1
和C3
,而第二列层级是我们想用foo
来索引的。
有时候我知道层级的名称,但不知道它们在多重索引中的位置。那在这种情况下,有没有办法使用多重索引切片呢?
举个例子,假设我知道我想在每个层级名称上应用什么切片,比如用一个字典表示:
'level_name_1' -> ':'
'level_name_2' -> ':'
'level_name_3' -> ['C1', 'C3']
但我不知道这些层级在多重索引中的位置(深度)。Pandas有没有内置的索引机制来处理这个问题?
如果我知道层级名称,但不知道它们的位置,我还能以某种方式使用pd.IndexSlice
对象吗?
补充一下:我知道我可以使用reset_index()
,然后就可以处理平坦的列,但我想避免重置索引(即使是暂时的)。我也可以使用query
,但query
要求索引名称必须符合Python的标识符(比如不能有空格等)。
我见过的最接近的例子是:
df.xs('C1', level='foo')
这里foo
是层级的名称,而C1
是我们感兴趣的值。
我知道xs
支持多个键,比如:
df.xs(('one', 'bar'), level=('second', 'first'), axis=1)
但它不支持切片或范围(像pd.IndexSlice
那样)。
3 个回答
很遗憾,.query()
这个方法不支持像普通切片那样的用法,但它可以通过名字选择索引级别,还能用区间选择!所以这也是你问题的另一个答案。
在使用查询时,可以通过反引号来引用索引名称,下面是示例。
# Get an example dataset from seaborn
import pandas as pd
import seaborn as sns
df = sns.load_dataset("penguins")
df = df.rename_axis("numerical index / ħ") # strange name to show escaping.
df = df.set_index(['species', 'island'], append=True)
# Working examples
# less than
df.query("`numerical index / ħ` < 100")
# range
slc = range(9, 90)
df.query("`numerical index / ħ` in @slc")
# Subsets
islands = ['Dream', 'Biscoe']
df.query("island in @islands and species == 'Adelie'")
我用一个自定义的函数来实现这个功能。这个函数叫做 sel
,名字是受到了 xarray 里同名方法的启发。
def sel(df, /, **kwargs):
"""
Select into a DataFrame by MultiIndex name and value
This function is similar in functionality to pandas .xs() and even more similar (in interface) to xarray's .sel().
Example:
>>> index = pd.MultiIndex.from_product([['TX', 'FL', 'CA'],
... ['North', 'South']],
... names=['State', 'Direction'])
>>> df = pd.DataFrame(index=index,
... data=np.random.randint(0, 10, (6,4)),
... columns=list('abcd'))
>>> sel(df, State='TX')
a b c d
State Direction
TX North 5 5 9 5
South 0 6 8 2
>>> sel(df, State=['TX', 'FL'], Direction='South')
a b c d
State Direction
TX South 0 6 8 2
FL South 6 7 5 2
indexing syntax is index_name=indexer where the indexer can be:
- single index value
- slice by using the slice() function
- a list of index values
- other indexing modes supported by indivdual axes in .loc[]
Unnamed index levels can be selected using names _0, _1 etc where the number is the index level.
raises KeyError if an invalid index level name is used.
"""
# argument checking
available_names = [name or f'_{i}' for i, name in enumerate(df.index.names)]
extra_args = set(kwargs.keys()) - set(available_names)
if extra_args:
raise KeyError(f"Invalid keyword arguments, no index(es) {extra_args} in dataframe. Available indexes: {available_names}.")
# compute indexers per index level
index_sel = tuple(kwargs.get(name or f'_{i}', slice(None)) for i, name in enumerate(df.index.names))
if not index_sel:
index_sel = slice(None)
# Fixup for single level indexes
if len(df.index.names) == 1 and index_sel:
index_sel = index_sel[0]
return df.loc[index_sel, :]
这个问题仍然在持续改进中,详细信息可以查看这里。支持这个功能其实很简单,欢迎大家提交改进建议!
你可以通过以下方法轻松解决这个问题:
In [11]: midx = pd.MultiIndex.from_product([list(range(3)),['a','b','c'],pd.date_range('20130101',periods=3)],names=['numbers','letters','dates'])
In [12]: midx.names.index('letters')
Out[12]: 1
In [13]: midx.names.index('dates')
Out[13]: 2
下面是一个完整的示例
In [18]: df = DataFrame(np.random.randn(len(midx),1),index=midx)
In [19]: df
Out[19]:
0
numbers letters dates
0 a 2013-01-01 0.261092
2013-01-02 -1.267770
2013-01-03 0.008230
b 2013-01-01 -1.515866
2013-01-02 0.351942
2013-01-03 -0.245463
c 2013-01-01 -0.253103
2013-01-02 -0.385411
2013-01-03 -1.740821
1 a 2013-01-01 -0.108325
2013-01-02 -0.212350
2013-01-03 0.021097
b 2013-01-01 -1.922214
2013-01-02 -1.769003
2013-01-03 -0.594216
c 2013-01-01 -0.419775
2013-01-02 1.511700
2013-01-03 0.994332
2 a 2013-01-01 -0.020299
2013-01-02 -0.749474
2013-01-03 -1.478558
b 2013-01-01 -1.357671
2013-01-02 0.161185
2013-01-03 -0.658246
c 2013-01-01 -0.564796
2013-01-02 -0.333106
2013-01-03 -2.814611
这是你的字典,里面存的是层级名称和切片的对应关系
In [20]: slicers = { 'numbers' : slice(0,1), 'dates' : slice('20130102','20130103') }
这段代码创建了一个空的索引器(也就是选择所有内容)
In [21]: indexer = [ slice(None) ] * len(df.index.levels)
接下来添加你的切片器
In [22]: for n, idx in slicers.items():
indexer[df.index.names.index(n)] = idx
然后进行选择(这里需要用元组,但最开始是用列表,因为我们需要修改它)
In [23]: df.loc[tuple(indexer),:]
Out[23]:
0
numbers letters dates
0 a 2013-01-02 -1.267770
2013-01-03 0.008230
b 2013-01-02 0.351942
2013-01-03 -0.245463
c 2013-01-02 -0.385411
2013-01-03 -1.740821
1 a 2013-01-02 -0.212350
2013-01-03 0.021097
b 2013-01-02 -1.769003
2013-01-03 -0.594216
c 2013-01-02 1.511700
2013-01-03 0.994332