Replacing a particular integer by another integer in pandas dataframe

pandas
datamanipulation
python

#1

Hi,
I have a data-set consisting of a column consisting of years (from 1996 to 2010), after exploration I have found that there are many observations having values equal to 0, which is an outlier. I want to replace the 0’s with 2010 and I have used the following code :

features[features.construction_year==0].construction_year=2010

Above code is not replacing the 0’s with 2010. Please help !!
Thanks in advance


#2

@ravi_6767,
Try this code :

features.construction_year[features[features.construction_year==0].index]=2010

As for the code you are using, the problem is that is every time it is creating a new data frame and not modifying your actual data frame. Go through this link, I am sure this will help you.
Regards,
Danish


#3

Hi @ravi_6767:

There is a simple syntax in pandas for this task:

data[‘construction_year’]=data[‘construction_year’].replace(to_replace=0,value=2010)

Enjoy!