-1

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.

surge3333
  • 133
  • 1
  • 8
  • 1
    Does this answer your question? [How can I pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-can-i-pivot-a-dataframe) – SomeDude Feb 03 '23 at 20:51

2 Answers2

1

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

df.set_index(['ID','TAG'])['VALUE'].unstack().reset_index()
rhug123
  • 7,893
  • 1
  • 9
  • 24
0

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
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105