I have the following dataframe :
datas = [['RAC1','CD0287',1.52,1.40,1.45,1.51], ['RAC1','CD0695',2.08,1.40,1.45,1.51], ['RAC1','ADN103-1',2.01,1.40,1.45,1.51], ['RAC3','CD0258',1.91,1.38,1.43,1.45], ['RAC3','ADN103-3',1.66,1.38,1.43,1.45], ['RAC8','CD0558',1.32,1.42,1.48,1.53], ['RAC8','ADN103-8',2.89,1.42,1.48,1.53]]
labels = ['Plate', 'Sample', 'LogRatio', 'm1', 'm2', 'm3']
df = pd.DataFrame(data = datas, columns=labels)
Plate Sample LogRatio m1 m2 m3
RAC1 CD0287 1.52 1.40 1.45 1.51
RAC1 CD0695 2.08 1.40 1.45 1.51
RAC1 ADN103-1 2.01 1.40 1.45 1.51
RAC3 CD0258 1.91 1.38 1.43 1.45
RAC3 ADN103-3 1.66 1.38 1.43 1.45
RAC8 CD0558 1.32 1.42 1.48 1.53
RAC8 ADN103-8 2.89 1.42 1.48 1.53
I would like to add a new column to calcul the mean M of m1, m2, m3 AND the value LogRatio of ADN103 but I don't know how to add just a value by plate. What I want is :
df['M'] = (df['m1'] + df['m2'] + df['m3'] + LogRatio_ADN103_of_the_plate)/4
For example for the first line of my dataframe, the calcul is :
df['M'] = (1.40 + 1.45 + 1.51 + 2.01) / 4
Plate Sample LogRatio m1 m2 m3 M
RAC1 CD0287 1.52 1.40 1.45 1.51 1,5925
RAC1 CD0695 2.08 1.40 1.45 1.51
RAC1 ADN103-1 2.01 1.40 1.45 1.51
RAC3 CD0258 1.91 1.38 1.43 1.45
RAC3 ADN103-3 1.66 1.38 1.43 1.45
RAC8 CD0558 1.32 1.42 1.48 1.53
RAC8 ADN103-8 2.89 1.42 1.48 1.53
Because 2.01 is the LogRatio value of ADN103 on plate RAC1. I know how to get the ADN103 value for all plates :
expreg = "ADN103_RAC."
ADN103 = df[df['Sample'].str.contains(expreg, regex=True)]
logRatio_ADN103 = ADN103['Log Ratio']
I tried a transformation in a new column by selecting only ADN103 values but I can't get their LogRatio value, it just retrun a boolean
df['ADN103oftheplate'] = df.groupby('Plate')['Sample'].transform(lambda x: x.str.contains(expreg, regex=True))
I don't know if it's clear. I tried so many ways I'm totally lost now.
Thanks for any help.