24

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?

Ilia Chigogidze
  • 1,595
  • 2
  • 8
  • 10

2 Answers2

50

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

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

See examples in docs.

Asclepius
  • 49,954
  • 14
  • 144
  • 128
zipa
  • 26,044
  • 6
  • 38
  • 55
7

You need to do:

area = area.replace(0, np.nan)
drec4s
  • 7,718
  • 8
  • 29
  • 49