user2896120
user2896120

Reputation: 3282

Pivoting column while retaining all other columns

I have many columns in a table, but only one column that needs to be pivoted with its values. It looks like this:

OrderNumber  Item  YearMonth  Total
1            1     2019_01    20
1            2     2019_01    40
1            1     2019_02    30
2            1     2019_02    50

The resulting output should be:

OrderNumber  Item  2019_01    2019_02   Total
1            1     60         30        20
1            2     60         30        40
1            1     60         30        30
2            1     0          50        50

Basically, sum all the total for each month's order number while retaining all columns. Is there a way to do this? I'm using Pandas

Upvotes: 1

Views: 52

Answers (2)

G.G
G.G

Reputation: 765

df.join(
    df.groupby(['OrderNumber', 'YearMonth'])['Total'].sum()
        .unstack(level=1,fill_value=0)
    , on='OrderNumber')
    
       OrderNumber  Item YearMonth  Total  2019_01  2019_02
    0            1     1   2019_01     20     60.0     30.0
    1            1     2   2019_01     40     60.0     30.0
    2            1     1   2019_02     30     60.0     30.0
    3            2     1   2019_02     50      0.0     50.0

Upvotes: 0

mozway
mozway

Reputation: 262164

IIUC, you need a pivot_table + merge:

out = (df
  .merge(df.pivot_table(index='OrderNumber', columns='YearMonth',
                        values='Total', aggfunc='sum', fill_value=0),
         on='OrderNumber')
  #.drop(columns='YearMonth') # uncomment to drop unused 'YearMonth'
  )

Output:

   OrderNumber  Item YearMonth  Total  2019_01  2019_02
0            1     1   2019_01     20       60       30
1            1     2   2019_01     40       60       30
2            1     1   2019_02     30       60       30
3            2     1   2019_02     50        0       50

Upvotes: 3

Related Questions