0

I am looking for a solution to the problem posted in:

Transform unique values of a column into multiple columns containing their corresponding values in another column

however in PYTHON.

Thanks!

Joe
  • 53
  • 6

2 Answers2

2

You need df.pivot_table():

df.pivot_table(index='ID',columns='Fruit',values='n',fill_value=0)

Fruit  Apple  Banana  Orange  Pear  Plum
ID                                      
1000       0       1       3     1     0
1001       0       1       0     0     2
1002       0       1       0     0     0
1003       0       1       2     1     1
1004       2       2       1     1     0
anky
  • 74,114
  • 11
  • 41
  • 70
  • And what if I do not how ID, but instead 3 different columns sex, age, date on which unique combinations I would like to merge my values? – Joe Jun 03 '19 at 09:37
  • @Joe sorry i didnt get it, is it there in the link you provided? – anky Jun 03 '19 at 09:39
  • I have slightly different data then the one with fruits however the problem is identical. In the fruits you merge the data on unique ID. In my data, the role of id are unique combinations of values from three different columns - (sex, age, date) as I work on population data. – Joe Jun 03 '19 at 09:45
  • @Joe i am not getting it, if it is similar, i advise you edit the question with a sample input and output data, if not please post a fresh question as the question posted is answered. – anky Jun 03 '19 at 09:47
1
data = {'id' : 
[1000,1000,1000,1001,10001,1002,1003,1003,1003,1003,1004,1004,1004,1004],
'Fruit' :['Banana', 'Orange', 'Pear', 'Banana', 'Plum', 'Banana', 'Banana', 'Orange', 
'Pear', 'Plum', 'Apple', 'Banana', 'Orange', 'Pear'],
'num' : [1,3,1,1,2,1,1,2,1,1,2,2,1,1]}

df = pd.DataFrame(data)

import numpy as np
df.pivot(index='id', columns='Fruit', values='num').replace(np.nan, 0)
Anna Semjén
  • 787
  • 5
  • 14