Pandas combining sparse columns in dataframe

后端 未结 5 1642
陌清茗
陌清茗 2021-01-18 03:44

I am using Python, Pandas for data analysis. I have sparsely distributed data in different columns like following

| id | col1a | col1b | col2a | col2b | col3a         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 04:35

    You can use df.stack() assuming 'id' is your index else set 'id' as index. Then use pd.pivot_table.

    df = df.stack().reset_index(name='val',level=1)
    df['group'] = 'g'+ df['level_1'].str.extract('col(\d+)')
    df['level_1'] = df['level_1'].str.replace('col(\d+)','')
    df.pivot_table(index=['id','group'],columns='level_1',values='val')
    
    level_1    cola  colb
    id group
    1  g1      11.0  12.0
    2  g2      21.0  86.0
    3  g1      22.0  87.0
    4  g3     545.0  32.0
    

提交回复
热议问题