0

I have a pandas dataframe as:

rater audio rating
    a   n1   y1
    a   n2   y2
    b   n1   y3
    b   n2   y4

I want to split the column rater to get

audio rating_a rating_b 
  n1    y1    y3
  n2    y2    y4

I tried a code as in Pandas: break categorical column to multiple columns

dftemp = dffinal.set_index(['rater', 'audio'])
dftemp.unstack(['rater'])

But this throws an error

Index contains duplicate entries, cannot reshape

As mentioned in Pandas unstack problems: ValueError: Index contains duplicate entries, cannot reshape

I tried using dftemp = dffinal.set_index(['rater', 'audio'],append=True) But this leads to lot of NaN values in the final output

      audio rating
rater      a    b
0    n1    y1  NaN
1    n2   NaN   y2
2    n1    y3  NaN
3    n2   NaN   y4

Similar question which I can't follow is: Pandas - unstack column values into new columns

Thanks jezrael, the solution is:

dffinal.pivot_table(index='audio', columns='rater', values='rating')
Kamini
  • 31
  • 3

0 Answers0