0

We have a dataframe looks like:

ID Gender  Lymphocyes Monocytes NRBC 
1    1      0.3        0.4      0
1    1      0.7        0.3      1
2    2       2         0.2      1
2    2      0.9        0.5      0

We know that for each ID we have only two results (meaning only 2 rows per ID) per blood test (Lymphocyes, Monocytes,NRBC) and we want to have only one row per ID as :

ID Gender Lymphocyes 1  Lymphocyes 2 Monocytes 1  Monocytes 2  NRBC 1  NRBC 2
1    1        0.3             0.7        0.4           0.3         0      1
2    2         2              0.9        0.2           0.5         1      0
David
  • 23
  • 5
  • 1
    `out = df.assign(k=df.groupby("ID").cumcount().add(1)).set_index(['ID','k']).unstack()` then `out.columns = out.columns.map("{0[0]} {0[1]}".format)` should do it. refer the examples in the link provided as duplicate – anky Jan 12 '21 at 18:47
  • Woua very impressive. I just forgot to precise that for each ID we also have a Gender_Code like 1 for Man and 2 for Woman. I just updated the dataframe out. – David Jan 12 '21 at 20:04
  • 1
    Add gender to groupby and add gender to set index too in the above code – anky Jan 12 '21 at 20:24

0 Answers0