Using unstack to reshape a python dataframe

Question:

I’m looking to reformat a dataframe by moving some of the rows to be columns. I’m trying to use unstack for this and not seeing the results I expected.

My input looks like this:

data = {'ID': ['Tom', 'Tom', 'Tom', 'Dick', 'Dick', 'Dick'],
        'TAG': ['instance', 'deadline', 'job', 'instance', 'deadline', 'job'],
        'VALUE': ['AA', '23:30', 'job01', 'BB', '02:15', 'job02']
        }
df = pd.DataFrame(data)

Giving me this:

     ID       TAG  VALUE
0   Tom  instance     AA
1   Tom  deadline  23:30
2   Tom       job  job01
3  Dick  instance     BB
4  Dick  deadline  02:15
5  Dick       job  job02

What I’m after is something that looks like this:

ID    instance  deadline  job
Tom   AA        23:30     job01
Dick  BB        02:15     job02

Using unstack as follows:

df = df.unstack().unstack()

I’m getting this:

              0         1      2         3         4      5
ID          Tom       Tom    Tom      Dick      Dick   Dick
TAG    instance  deadline    job  instance  deadline    job
VALUE        AA     23:30  job01        BB     02:15  job02

Appreciate any assistance here in getting the desired results.

Asked By: surge3333

||

Answers:

You are looking for df.pivot:

df = df.pivot(index='ID', columns='TAG', values='VALUE')
print(df)

TAG  deadline instance    job
ID                           
Dick    02:15       BB  job02
Tom     23:30       AA  job01
Answered By: RomanPerekhrest

This will work if you would like to use unstack()

df.set_index(['ID','TAG'])['VALUE'].unstack().reset_index()
Answered By: rhug123
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.