close

How to replace a value in pandas, with NaN?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to replace a value in pandas, with NaN in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to replace a value in pandas, with NaN?

  1. How to replace a value in pandas, with NaN?

    OK I figured out your problem, by default if you don't pass a separator character then read_csv will use commas ',' as the separator.

  2. replace a value in pandas, with NaN

    OK I figured out your problem, by default if you don't pass a separator character then read_csv will use commas ',' as the separator.

Method 1

You can replace this just for that column using replace:

df['workclass'].replace('?', np.NaN)

or for the whole df:

df.replace('?', np.NaN)

UPDATE

OK I figured out your problem, by default if you don’t pass a separator character then read_csv will use commas ',' as the separator.

Your data and in particular one example where you have a problematic line:

54, ?, 180211, Some-college, 10, Married-civ-spouse, ?, Husband, Asian-Pac-Islander, Male, 0, 0, 60, South, >50K

has in fact a comma and a space as the separator so when you passed the na_value=['?'] this didn’t match because all your values have a space character in front of them all which you can’t observe.

if you change your line to this:

rawfile = pd.read_csv(filename, header=None, names=DataLabels, sep=',\s', na_values=["?"])

then you should find that it all works:

27      54               NaN  180211  Some-college             10 

Method 2

okay I got it by :

 #========trying to replace ?
    newraw= rawfile.replace('[?]', np.nan, regex=True)
    print newraw[25:40]

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read