Pandas combining sparse columns in dataframe

后端 未结 5 1644
陌清茗
陌清茗 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:14

    Another alternative with pd.wide_to_long

    m = pd.wide_to_long(df,['col'],'id','j',suffix='\d+\w+').reset_index()
    
    (m.join(pd.DataFrame(m.pop('j').agg(list).tolist()))
      .assign(group=lambda x:x[0].radd('g'))
      .set_index(['id','group',1])['col'].unstack().dropna()
      .rename_axis(None,axis=1).add_prefix('col').reset_index())
    

       id group cola colb
    0   1    g1   11   12
    1   2    g2   21   86
    2   3    g1   22   87
    3   4    g3  545   32
    

提交回复
热议问题