Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transposing data that is grouped by Date

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading