Transposing a column in a pandas dataframe while keeping other column intact with duplicates

后端 未结 2 1441
Happy的楠姐
Happy的楠姐 2020-11-29 12:50

My data frame is as follows

selection_id  last_traded_price
430494        1.46
430494        1.48
430494        1.56
430494        1.57
430495        2.45
43         


        
2条回答
  •  遥遥无期
    2020-11-29 13:33

    You can use cumcount for Counter for new columns names created by set_index + unstack or pandas.pivot:

    g = df.groupby('selection_id').cumcount()
    df = df.set_index(['selection_id',g])['last_traded_price'].unstack()
    print (df)
                     0     1     2     3
    selection_id                        
    430494        1.46  1.48  1.56  1.57
    430495        2.45  2.67  2.72  2.87
    

    Similar solution with pivot:

    df = pd.pivot(index=df['selection_id'], 
                  columns=df.groupby('selection_id').cumcount(), 
                  values=df['last_traded_price'])
    print (df)
                     0     1     2     3
    selection_id                        
    430494        1.46  1.48  1.56  1.57
    430495        2.45  2.67  2.72  2.87
    

提交回复
热议问题