I would like to normalize my data in a Pandas DataFrame grouped by Type
with the mean of the values that are in the Condition
CT
.
The DataFrame is something like this:
df = pd.DataFrame({'Type' : ['A', 'A', 'A', 'A',
'B', 'B', 'B', 'B'],
'Condition' : ['Tx', 'CT', 'Tx', 'CT',
'Tx', 'CT', 'Tx', 'CT'],
'Var1' : np.random.randn(8),
'Var2' : np.random.randn(8)})
print(df)
Condition Type Var1 Var2 Var1_Norm Var2_Norm
0 Tx A -1.555886 -0.454512 3.290695 -1.059712
1 CT A 0.820324 0.357123 -1.734983 0.832645
2 Tx A -0.355758 0.807324 0.752426 1.882305
3 CT A -0.799936 1.005673 1.691862 2.344762
4 Tx B -0.253152 -0.585186 0.234666 6.790024
5 CT B -0.672658 0.851191 0.623540 -9.876536
6 Tx B -1.768877 -0.083506 1.639711 0.968933
7 CT B -1.620407 -0.527232 1.502083 6.117579
I know how to normalize by the mean of the entire group:
df[['Var1_Norm', 'Var2_Norm']] = df.groupby(['Type']).transform(lambda x: x/x.mean())
But how do I normalize the grouped data by the mean of a subset of the group (rows with Condition == 'CT'
?
I tried the following which results in an AttributeError
:
df[['Var1_Norm', 'Var2_Norm']] = df.groupby(['Type']).transform(lambda x: x/x[x.Condition == 'CT'].mean())
AttributeError: ("'Series' object has no attribute 'Condition'", 'occurred at index Condition')
With the help of @piRSquared's answer I found a solution using a for loop:
df[['Var1_Norm', 'Var2_Norm']] = df[['Var1', 'Var2']]
for t in df.Type.unique():
ct_mean = df.loc[(df.Type == t) & (df.Condition == 'CT'),['Var1_Norm', 'Var2_Norm']].mean()
df.loc[df.Type == t,['Var1_Norm', 'Var2_Norm']] = df.loc[df.Type == t,['Var1_Norm', 'Var2_Norm']].div(ct_mean)