通过使用pandas连接两个不同级别的标签来创建列名

时间:2014-02-09 00:49:05

标签: python pandas dataframe rename

有没有办法通过连接表中不同级别的两个标签直接创建列名? 例如,假设我有这个数据框:

df = pd.DataFrame({'n':[0 ,1 ,0 ,0 ,1 ,1 ,0 ,1],'l':[12 ,16 ,92, 77 ,32 ,47, 22, 14], 'cols':['col1','col1','col1','col1','col2','col2','col2','col2'], 'index':range(4)*2})
df_p = df.pivot(index='index', columns='cols')

我想将l / n添加到col1 / col2。最后,我应该有与列一样多的名称:

    col1_l   col2_l   col1_n   col2_n
    12       32       0        1
    16       47       1        1
    92       22       0        0
    77       14       0        1

我知道我可以这样做:

names_l = df_p.l.columns + '_l'
names_n = df_p.n.columns + '_n' 
df_p.columns = names_l.append(names_n)

但也许有一种更直接的方法可以做到这一点,我不知道。

1 个答案:

答案 0 :(得分:3)

种不同的方式。您希望以相反的顺序组合列多索引的级别名称,并将它们与_连接起来,因此您觉得必须至少指定三项内容。无论如何,您可以删除对名称的依赖,例如

>>> df_p.columns = ['_'.join(c[::-1]) for c in df_p.columns]
>>> df_p
       col1_l  col2_l  col1_n  col2_n
index                                
0          12      32       0       1
1          16      47       1       1
2          92      22       0       0
3          77      14       0       1

[4 rows x 4 columns]

这是有效的,因为当你迭代原始列时,你得到

>>> list(df_p.columns)
[('l', 'col1'), ('l', 'col2'), ('n', 'col1'), ('n', 'col2')]