Skip to content Skip to sidebar Skip to footer

Pandas Groupby And Correct With Median In New Column

My dataframe look like this Plate Sample LogRatio P1 S1 0.42 P1 S2 0.23 P2 S3 0.41 P3 S4 0.36 P3 S5 0.18 I have calculated the medi

Solution 1:

You can use transform:

df['CorrectedLogRatio'] = df['LogRatio'] - df.groupby('Plate')['LogRatio'].transform('median')

The resulting output:

  Plate Sample  LogRatio  CorrectedLogRatio
0    P1     S1      0.42              0.095
1    P1     S2      0.23             -0.095
2    P2     S3      0.41              0.000
3    P3     S4      0.36              0.090
4    P3     S5      0.18             -0.090

Post a Comment for "Pandas Groupby And Correct With Median In New Column"