Let’s say I have 600,000 data points in column for age. In the data there are values 0 and -1, which is not relevant for age. How can I change both 0 and -1 values in my data to the column mean value using python?
The code so far:
JavaScript
x
4
1
df6 = df5['Vict Age'].replace([0, -1]).mean())
2
df6.update(df5)
3
df6
4
Advertisement
Answer
You can find the mean separatly and then use the correct replace syntax to replace desired values:
JavaScript
1
5
1
# Calculate mean ignoring -1, 0 values
2
age_mean = df5['Vict Age'][~df5['Vict Age'].isin([-1,0])].mean()
3
# Replace -1, 0 values
4
df5['Vict Age'] = df5['Vict Age'].replace({0: age_mean , -1: age_mean})
5
PS: Please use Stack Overflow code formatting instead of posting the image in the future. Thanks.