Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick which column to unstack a dataframe on

I have a data set that looks like:

UniqueID CategoryType Value
   A         Cat1      apple
   A         Cat2      banana
   B         Cat1      orange
   C         Cat2      news
   D         Cat1      orange
   D         Cat2      blue

I'd like it to look like:

UniqueID Cat1 Cat2
   A     apple banana
   B     orange
   C           news
   D     orange blue

I've tried using unstack, but can't get the right index set or something.

Thanks

like image 514
bjurstrs Avatar asked Jan 30 '23 15:01

bjurstrs


1 Answers

The bulk of the work is done with

df.set_index(['UniqueID', 'CategoryType']).Value.unstack(fill_value='')

CategoryType    Cat1    Cat2
UniqueID                    
A              apple  banana
B             orange        
C                       news
D             orange    blue

We can get the rest of the formatting with

df.set_index(['UniqueID', 'CategoryType']).Value.unstack(fill_value='') \
    .rename_axis(None, 1).reset_index()

  UniqueID    Cat1    Cat2
0        A   apple  banana
1        B  orange        
2        C            news
3        D  orange    blue
like image 83
piRSquared Avatar answered Feb 27 '23 20:02

piRSquared