Home > Enterprise >  Groupby: how to compute a tranformation and division in every value by group
Groupby: how to compute a tranformation and division in every value by group

Time:11-19

I have a database like this:

 participant  time1  time2 ... time27
           1  0.003  0.001      0.003
           1  0.003  0.002      0.001
           1  0.006  0.003      0.003
           1  0.003  0.001      0.003
           2  0.003  0.003      0.001
           2  0.003  0.003      0.001
           3  0.006  0.003      0.003
           3  0.007  0.044      0.006
           3  0.000  0.005      0.007

I need to perform a transformation using np.log1p() per participant and divide every value by the maximum of each participant.

(log [X 1]) / Xmax

How can I do this?

CodePudding user response:

You can use:

df.join(df.groupby('participant')
          .transform(lambda s: np.log1p(s)/s.max())
          .add_suffix('_trans')
       )

Output (as new columns):

   participant  time1  time2  time27  time1_trans  time2_trans  time27_trans
0            1  0.003  0.001   0.003     0.499251     0.333167      0.998503
1            1  0.003  0.002   0.001     0.499251     0.666001      0.333167
2            1  0.006  0.003   0.003     0.997012     0.998503      0.998503
3            1  0.003  0.001   0.003     0.499251     0.333167      0.998503
4            2  0.003  0.003   0.001     0.998503     0.998503      0.999500
5            2  0.003  0.003   0.001     0.998503     0.998503      0.999500
6            3  0.006  0.003   0.003     0.854582     0.068080      0.427930
7            3  0.007  0.044   0.006     0.996516     0.978625      0.854582
8            3  0.000  0.005   0.007     0.000000     0.113353      0.996516
  • Related