Python: replacing outliers values with median values
I think this is what you are looking for, you can use loc to assign value . Then you can fill the nan
median = df.loc[df['Age']<75, 'Age'].median()
df.loc[df.Age > 75, 'Age'] = np.nan
df.fillna(median,inplace=True)
You can also use np.where in one line
df["Age"] = np.where(df["Age"] >75, median,df['Age'])
You can also use .mask i.e
df["Age"] = df["Age"].mask(df["Age"] >75, median)