另一个答案使用from_tuples()
. 这概括了这个先前的答案。
key = "Foo"
name = "First"
# If df.index.nlevels > 1:
df.index = pd.MultiIndex.from_tuples(((key, *item) for item in df.index),
names=[name]+df.index.names)
# If df.index.nlevels == 1:
# df.index = pd.MultiIndex.from_tuples(((key, item) for item in df.index),
# names=[name]+df.index.names)
我喜欢这种方法,因为
- 它只修改索引(没有不必要的正文复制操作)
- 它适用于两个轴(行和列索引)
- 它仍然可以写成单行
将上述内容包装在一个函数中可以更轻松地在行索引和列索引之间以及单级和多级索引之间切换:
def prepend_index_level(index, key, name=None):
names = index.names
if index.nlevels==1:
# Sequence of tuples
index = ((item,) for item in index)
tuples_gen = ((key,)+item for item in index)
return pd.MultiIndex.from_tuples(tuples_gen, names=[name]+names)
df.index = prepend_index_level(df.index, key="Foo", name="First")
df.columns = prepend_index_level(df.columns, key="Bar", name="Top")
# Top Bar
# Vals
# First A B
# Foo a1 b1 -0.446066
# b2 -0.248027
# a2 b3 0.522357
# a3 b4 0.404048
最后,可以通过在任何索引级别插入键来进一步概括上述内容:
def insert_index_level(index, key, name=None, level=0):
def insert_(pos, seq, value):
seq = list(seq)
seq.insert(pos, value)
return tuple(seq)
names = insert_(level, index.names, name)
if index.nlevels==1:
# Sequence of tuples.
index = ((item,) for item in index)
tuples_gen = (insert_(level, item, key) for item in index)
return pd.MultiIndex.from_tuples(tuples_gen, names=names)
df.index = insert_index_level(df.index, key="Foo", name="Last", level=2)
df.columns = insert_index_level(df.columns, key="Bar", name="Top", level=0)
# Top Bar
# Vals
# A B Last
# a1 b1 Foo -0.595949
# b2 Foo -1.621233
# a2 b3 Foo -0.748917
# a3 b4 Foo 2.147814