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.
CodePudding user response:
This will work if you would like to use unstack()
df.set_index(['ID','TAG'])['VALUE'].unstack().reset_index()
CodePudding user response:
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