Python: replacing outliers values with median values

Viewed 37834

I have a python data-frame in which there are some outlier values. I would like to replace them with the median values of the data, had those values not been there.

id         Age
10236    766105
11993       288
9337        205
38189        88
35555        82
39443        75
10762        74
33847        72
21194        70
39450        70

So, I want to replace all the values > 75 with the median value of the dataset of the remaining dataset, i.e., the median value of 70,70,72,74,75.

I'm trying to do the following:

  1. Replace with 0, all the values that are greater than 75
  2. Replace the 0s with median value.

But somehow, the below code not working

df['age'].replace(df.age>75,0,inplace=True)
4 Answers

A more general solution I've tried lately: replace 75 with the median of the whole column and then follow a solution similar to what Bharath suggested:

median = float(df['Age'].median())
df["Age"] = np.where(df["Age"] > median, median, df['Age'])

you code is almost right , but their is a gap.
use:

df['age']=df['age'].replace(df.age>75,0,inplace=True)
Related