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

How to transpose certain columns in a pandas dataframe

Currently have a dataframe like this:

RepName Hours Reason
John 4 Research
John 15 Training
Matt 6 Project Labor
Matt 10 Training

I want to transpose each reason as a column with the Hours values as values like so:

RepName Research Training Project Labor
John 4 15 0
Matt 0 10 6

I tried to transpose and melting the dataframe and couldn’t figure it out. Still sort of new to this, any help would be appreciated.

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 :

You can do this with pd.pivot_table like so:

>>> df.pivot_table(index="RepName", columns="Reason", values="Hours", fill_value=0)
Reason   Project Labor  Research  Training
RepName
John                 0         4        15
Matt                 6         0        10

Full example:

import pandas as pd

input_data = pd.DataFrame(
    {
        "RepName": ["John", "John", "Matt", "Matt"],
        "Hours": [4, 15, 6, 10],
        "Reason": ["Research", "Training", "Project Labor", "Training"],
    }
)

output_data = pd.DataFrame(
    {
        "Project Labor": [0, 6],
        "Research": [4, 0],
        "Training": [15, 10],
    },
    index=["John", "Matt"],
)

pivoted = input_data.pivot_table(
    index="RepName",
    columns="Reason",
    values="Hours",
    fill_value=0,
)

assert output_data.equals(pivoted)
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