Replace None with NaN in pandas dataframe

3.5K    Asked by alexGONZALEZ in Data Science , Asked on Jul 8, 2021

I have table x:

        website
0   http://www.google.com/
1   http://www.yahoo.com
2   None

I want to replace python None with pandas NaN. I tried:

x.replace(to_replace=None, value=np.nan)

But I got:

TypeError: 'regex' must be a string or a compiled regular expression or a list or dict of strings or regular expressions, you passed a 'bool'

How pandas replace nan with none? How should I go about it?


Answered by Chris Dyer

 Use DataFrame.fillna or Series.fillna which will help in replacing the Python object None, not the string 'None'.

import pandas as pd

For dataframe:

df.fillna(value=pd.np.nan, inplace=True)

For column or series:

df.mycol.fillna(value=pd.np.nan, inplace=True)



Your Answer

Interviews

Parent Categories