Replace None with NaN in pandas dataframe
You can use DataFrame.fillna
or Series.fillna
which will replace the Python object None
, not the string 'None'
.
import pandas as pd
import numpy as np
For dataframe:
df = df.fillna(value=np.nan)
For column or series:
df.mycol.fillna(value=np.nan, inplace=True)
Here's another option:
df.replace(to_replace=[None], value=np.nan, inplace=True)
The following line replaces None
with NaN
:
df['column'].replace('None', np.nan, inplace=True)