I am trying to implement mean normalization of rows in pandas. Find the mean of every row in pandas, subtract the mean from each element for the particular row.
Code:
df = pd.DataFrame(np.random.randint(0,100,size=(4, 5)), columns=list('ABCDE'))
print (df)
A B C D E
0 53 77 34 51 41
1 44 46 6 70 31
2 52 22 95 88 13
3 77 18 88 86 20
x = pd.DataFrame(df.mean(axis = 1),columns=['mean'])
for index,rows in df.iterrows():
for i in range(len(x)):
df.loc[index] = df.loc[index] - x.loc[i]
print (df)
op:
A B C D E
0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN NaN NaN NaN
3 NaN NaN NaN NaN NaN
Any suggestions on what's the mistake