Home > other >  How to replace irrelevant data into mean values?
How to replace irrelevant data into mean values?

Time:12-28

Let's say I'm having 600,000 data in column for age. In the data im having values 0 and -1 as age which is not relevant. So how can I change both 0 and -1 values in my data to its column mean value?

how to solve this problem using python coding

The code so far:

df6 = df5['Vict Age'].replace([0, -1]).mean())
df6.update(df5)
df6

CodePudding user response:

You can find the mean separatly and then use the correct replace syntax to replace desired values:

age_mean = df5['Vict Age'].mean()
df5['Vict Age'] = df5['Vict Age'].replace({0: age_mean , 1: age_mean})

PS: Please use Stack Overflow code formatting instead of posting the image in the future. Thanks.

  • Related