使用分层列更改Pandas中的列名称 [英] Changing columns names in Pandas with hierarchical columns

查看:112
本文介绍了使用分层列更改Pandas中的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有分层索引的数据框:

Let's say I have a dataframe with a hierarchical index:

>>> df = pd.DataFrame(np.ones((2, 4)))
>>> df
   0  1  2  3
0  1  1  1  1
1  1  1  1  1

[2 rows x 4 columns]
>>> df.columns = pd.MultiIndex.from_product([['a', 'b'], ['i', 'ii']])
>>> df
   a      b    
   i  ii  i  ii
0  1   1  1   1
1  1   1  1   1

[2 rows x 4 columns]

是否有一种简单的方法来更改"b"下的列名?我认为以下内容很直观,但不起作用.

Is there an easy way to change the column names under 'b'? I thought the following was intuitive, but it does not work.

>>> df['b'].columns = ['iii', 'iv']
>>> df
   a      b    
   i  ii  i  ii
0  1   1  1   1
1  1   1  1   1

[2 rows x 4 columns]

推荐答案

重命名柱状多索引ATM有点笨拙(将其重命名为self不会很好地处理多索引).如果它是框架的索引,则很容易reset_index/rename/set_index.

Renaming a columar multi-index is a bit akward ATM (and rename it self it doesn't handle the multi-indexes in a nice way). If its an index of the frame, its pretty easy to reset_index/rename/set_index.

因此有2个选择:(在0.15.0中也将变得更容易);现在必须设置所有级别,即使您没有更改.

So 2 choices: (this will get easier in 0.15.0 as well); right now have to set all the levels, even the ones you are not changing.

In [39]: df.columns = df.columns.set_levels([['iii','iv'],df.columns.levels[1]])

In [40]: df
Out[40]: 
   iii      iv    
     i  ii   i  ii
0    1   1   1   1
1    1   1   1   1

或者最简单的方法是重新创建并设置新索引(针对所有列).

Or easiest simply to recreate and set a new index (for all the columns).

这篇关于使用分层列更改Pandas中的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆