python pandas:重命名多索引数据框中的单列标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29369568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas: rename single column label in multi-index dataframe
提问by Boosted_d16
I have a df that looks like this:
我有一个 df 看起来像这样:
df = pd.DataFrame(np.random.random((4,4)))
df.columns = pd.MultiIndex.from_product([['1','2'],['A','B']])
print df
1 2
A B A B
0 0.030626 0.494912 0.364742 0.320088
1 0.178368 0.857469 0.628677 0.705226
2 0.886296 0.833130 0.495135 0.246427
3 0.391352 0.128498 0.162211 0.011254
How can I rename column '1' and '2' as 'One' and 'Two'?
如何将列“1”和“2”重命名为“一”和“二”?
I thought df.rename() would've helped but it doesn't. Have no idea how to do this?
我认为 df.rename() 会有所帮助,但事实并非如此。不知道该怎么做?
回答by joris
That is indeed something missing in rename(ideally it should let you specify the level).
Another way is by setting the levels of the columns index, but then you need to know all values for that level:
这确实是缺少的东西rename(理想情况下它应该让您指定级别)。
另一种方法是设置列索引的级别,但是您需要知道该级别的所有值:
In [41]: df.columns.levels[0]
Out[41]: Index([u'1', u'2'], dtype='object')
In [43]: df.columns = df.columns.set_levels(['one', 'two'], level=0)
In [44]: df
Out[44]:
one two
A B A B
0 0.899686 0.466577 0.867268 0.064329
1 0.162480 0.455039 0.736870 0.759595
2 0.620960 0.922119 0.060141 0.669997
3 0.871107 0.043799 0.080080 0.577421
In [45]: df.columns.levels[0]
Out[45]: Index([u'one', u'two'], dtype='object')
回答by TheBlackCat
Use set_levels:
使用set_levels:
>>> df.columns.set_levels(['one','two'], 0, inplace=True)
>>> print(df)
one two
A B A B
0 0.731851 0.489611 0.636441 0.774818
1 0.996034 0.298914 0.377097 0.404644
2 0.217106 0.808459 0.588594 0.009408
3 0.851270 0.799914 0.328863 0.009914
回答by Cristián Antu?a
df.columns.set_levels(['one', 'two'], level=0, inplace=True)
回答by amn34
df.rename_axis({'1':'one', '2':'two'}, axis='columns', inplace=True)
df.rename_axis({'1':'one', '2':'two'}, axis='columns', inplace=True)
回答by kadee
As of pandas 0.22.0 (and probably much earlier), you can specify the level:
从 pandas 0.22.0(可能更早)开始,您可以指定级别:
df = df.rename(columns={'1': one, '2': two}, level=0)
df = df.rename(columns={'1': one, '2': two}, level=0)
or, alternatively (new notation since pandas 0.21.0):
或者,或者(自Pandas 0.21.0 以来的新符号):
df = df.rename({'1': one, '2': two}, axis='columns', level=0)
df = df.rename({'1': one, '2': two}, axis='columns', level=0)
But actually, it works even when omitting the level:
但实际上,即使省略级别它也能工作:
df = df.rename(columns={'1': one, '2': two})
df = df.rename(columns={'1': one, '2': two})
In that case, all column levels are checked for occurrences to be renamed.
在这种情况下,将检查所有列级别以查找要重命名的事件。
回答by Ying Zhang
This is a good question. Combining the answer above, you can write a function:
这是一个很好的问题。结合上面的答案,可以写一个函数:
def rename_col( df, columns, level = 0 ):
def rename_apply ( x, rename_dict ):
try:
return rename_dict[x]
except KeyError:
return x
if isinstance(df.columns, pd.core.index.MultiIndex):
df.columns = df.columns.set_levels([rename_apply(x, rename_dict = columns ) for x in df.columns.levels[level]], level= level)
else:
df.columns = [rename_apply(x, rename_dict = columns ) for x in df.columns ]
return df
It worked for me.
它对我有用。
Ideally, a functionality like this should be integrated into the "official" "rename" function in the future, so you don't need to write a hack like this.
理想情况下,这样的功能应该在未来集成到“官方”“重命名”功能中,因此您不需要编写这样的hack。

