Christopher Christopher - 1 year ago 79
Python Question

Reshape pandas dataframe from rows to columns

I'm trying to reshape my data. At first glance, it sounds like a transpose, but it's not. I tried melts, stack/unstack, joins, etc.

Use Case

I want to have only one row per unique individual, and put all job history on the columns. For clients, it can be easier to read information across rows rather than reading through columns.

Here's the data:

import pandas as pd
import numpy as np

data1 = {'Name': ["Joe", "Joe", "Joe","Jane","Jane"],
'Job': ["Analyst","Manager","Director","Analyst","Manager"],
'Job Eff Date': ["1/1/2015","1/1/2016","7/1/2016","1/1/2015","1/1/2016"]}
df2 = pd.DataFrame(data1, columns=['Name', 'Job', 'Job Eff Date'])


Here's what I want it to look like:
Desired Output Table

enter image description here

Answer Source

.T within groupby

def tgrp(df):
    """In preparation for the transpose I drop the name column and any index.
    This makes the recombining after groupby possible"""
    df = df.drop('Name', axis=1)
    return df.reset_index(drop=True).T


enter image description here