Home > database >  Transposing data that is grouped by Date
Transposing data that is grouped by Date

Time:10-08

I have a dataframe that looks like below

Date     Name                   
01-2021  Mark                     714.53
         Chris                 112681.49
         Ashley                  3127.07
         Brad                   16875.00
         Michelle              429520.04
                                 ...    
12-2021  Mark                  429520.04
         Chris                 975261.29
         Ashley                377449.79
         Brad                   53391.73
         Michelle                4286.00

But I need to transpose it like below:

Name        01-2021           12-2021
Mark         714.53         429520.04
Chris     112681.49         975261.29
Ashley      3127.07         377449.79
Brad       16875.00          53391.73
Michelle  429520.04           4286.00

Does anyone have a solution please.

CodePudding user response:

pd.pivot

# the last column is assumed as 'amt'

df.pivot(index='Name', columns='Date', values='amt').reset_index().rename_axis(columns=None)
    Name    01-2021     12-2021
0   Ashley  3127.07     377449.79
1   Brad    16875.00    53391.73
2   Chris   112681.49   975261.29
3   Mark    714.53      429520.04
4   Michelle 429520.04  4286.00
  • Related