I want to replace maximum value for each row in the column with mean
value for this row. the method i am using taking a lot of time for complete. i am using pandas DataFrame
. replaced mean value need to be an integer but with correct flood division.example: if value is 3.2 then 3 or if value is 3.8 then 4.
my slow solution:
for j in range(0,len(df_train)):
val = df_train.iloc[j,1:51].mean()
m = df_train.iloc[j,1:51].max()
df_train.iloc[j,1:51] = df_train.iloc[j,1:51].replace(m,int(val))
My DataFrame:
id | feature0 | feature1 | feature2 | feature3 | feature4 |
---|---|---|---|---|---|
0 | 0 | 0 | 3 | 1 | 5 |
1 | 4 | 0 | 4 | 0 | 8 |
2 | 1 | 21 | 4 | 0 | 0 |
3 | 0 | 11 | 0 | 0 | 2 |
Output i want:
id | feature0 | feature1 | feature2 | feature3 | feature4 |
---|---|---|---|---|---|
0 | 0 | 0 | 3 | 1 | 2 |
1 | 4 | 0 | 4 | 0 | 3 |
2 | 1 | 5 |
4 | 0 | 0 |
3 | 0 | 3 |
0 | 0 | 2 |