1

Struggling with an unstacking problem. I have a table in the following format:

Word    Number  MetaData    Label   Value
One     1       Hello       A       5a
One     1       Hello       B       2b
One     1       Hello       C       8c
Two     2       World       A       2a
Two     2       World       B       5b
Two     2       World       C       1c

I want to unstack it to this, preserving all of my other columns. In all cases, Word, Number, and MetaData will always be the same for each set of A, B, and C:

Word    Number  MetaData    A   B   C
One     1       Hello       5a  2b  8c
Two     2       World       2a  5b  1c
pacificdune
  • 95
  • 1
  • 10
  • You want pivoting, not unstacking. – DYZ Aug 23 '18 at 22:13
  • I edited my question to be more clear. Pivot seems to imply that I am summarizing the value field which. The closest example of what I am doing is a jmp unstack function while keeping all of my columns. I will resubmit the question with my edits. – pacificdune Aug 24 '18 at 15:36

1 Answers1

2

You want pivot_table here:

df.pivot_table(
    index=['Word', 'Number', 'MetaData'],
    columns='Label',
    values='Value'
).reset_index()

Label Word  Number MetaData  A  B  C
0      One       1    Hello  5  2  8
1      Two       2    World  2  5  1
user3483203
  • 50,081
  • 9
  • 65
  • 94