【问题标题】:create column names by joining two labels of different levels with pandas通过使用 pandas 连接两个不同级别的标签来创建列名
【发布时间】:2014-03-06 09:36:19
【问题描述】:

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

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)

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

【问题讨论】:

    标签: python pandas dataframe rename


    【解决方案1】:

    不同的方法。您想以相反的顺序组合多索引列的级别名称,并将它们与_ 连接起来,所以感觉您至少需要指定三件事。无论如何,您可以删除对名称的依赖,例如

    >>> 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')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2018-11-26
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多