Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't replace 0 to nan in Python using Pandas [duplicate]

I have dataframe with only 1 column. I want to replace all '0' to np.nan but I can't achieve that.

dataframe is called area. I tried:

area.replace(0,np.nan)
area.replace(to_replace=0,np.nan)
area.replace(to_replace=0,value=np.nan)

area.replace('0',np.nan)

What should I do?

like image 591
Ilia Chigogidze Avatar asked Mar 30 '18 14:03

Ilia Chigogidze


2 Answers

You can set inplace to True (default is False):

area.replace(0, np.nan, inplace=True)

See examples in docs.

like image 155
zipa Avatar answered Sep 27 '22 20:09

zipa


You need to do:

area = area.replace(0, np.nan)
like image 38
drec4s Avatar answered Sep 27 '22 20:09

drec4s