1

DF Description:

Pandas DF has two columns 'ID', 'Names', where there are multiple entries of 'Names' for the same 'ID'

Use Case:

IP:

 ID          Names
 1           func_A
 1           tunc_B
 2           rain
 2           fire

Required OP:

A new DF with the entry like following

ID    func_A       func_B   rain fire
 1        1         1         0   0
 2        0         0         1   1

I tried 2 approaches using groupBy and get_dummies but I am not getting the required op. Any suggestions on how to attain this highly appreciated. thanks.

data_person
  • 4,194
  • 7
  • 40
  • 75

1 Answers1

1

You can use pd.crosstab(df.ID, df.Names).

Output:

Names  fire  func_A  rain  tunc_B
ID
1         0       1     0       1
2         1       0     1       0
hilberts_drinking_problem
  • 11,322
  • 3
  • 22
  • 51