【问题标题】:create column name repeats for column values when particular columns have duplicate rows当特定列具有重复行时,为列值创建列名重复
【发布时间】:2019-01-22 13:04:20
【问题描述】:

我有一个需要旋转的数据框(不确定这是否涉及堆叠或旋转......)

所以,如果我在 "Year"、"Month" 和 "Group" 列中有重复值,我想将跟随列的名称转移到变量中重复

所以如果这是原来的DF:

Year  Month  Group  Variable  feature1  feature2  feature3  
2010    6      1      1           12        23        56
2010    6      1      2           34        56        25 

结果将是:

Year  Month  Group  Variable1  feature1_1  feature2_1  feature3_1  Variable2  feature1_2    feature2_2  feature3_2 
 2010    6      1      1           12        23        56               2           34           56       25

我正在寻找类似的东西 - 非常感谢任何提示/帮助,

谢谢

伊兹

【问题讨论】:

  • 不完全符合您的要求,但unstack 可能对您有用。它将创建分层列。

标签: python pandas dataframe stack pivot


【解决方案1】:

IIUC,如果你想将它从 long 转换回 wide ,你可以使用 cumcount 获取 addtional 键,然后 reshape。(注意 wide_to_long 的反面)

df['New']=(df.groupby(['Year','Month','Group']).cumcount()+1).astype(str)
w=df.set_index(['Year','Month','Group','New']).unstack().sort_index(level=1,axis=1)
w.columns=pd.Index(w.columns).str.join('_')
w
Out[217]: 
                  Variable_1  feature1_1  feature2_1  feature3_1  Variable_2  \
Year Month Group                                                               
2010 6     1               1          12          23          56           2   
                  feature1_2  feature2_2  feature3_2  
Year Month Group                                      
2010 6     1              34          56          25  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2020-07-08
    • 1970-01-01
    • 2013-06-22
    • 2012-09-26
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多