Replacing a particular integer by another integer in pandas dataframe

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

@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

1 Like

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!

1 Like

TypeError Traceback (most recent call last)
in
----> 1 df[‘flag’]=df[‘flag’].int().replace([-0.423666,2.341313],[0,1])

TypeError: ‘int’ object is not subscriptable

Looks like you are trying to update multiple values like on 0 some value and on 1 some value.
Is that correct ?
If is it so, then you must use map function and within that you can pass the values and items in dictionary.

Thanks,
Mahesh

Simple!!, try this
features.features.construction_year = features.features.construction_year.replace(0,2010)