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?

2

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

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

See examples in docs.

You need to do:

area = area.replace(0, np.nan)
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest