Pandas: How to Replace NaN with None


You can use the following basic syntax to replace NaN values with None in a pandas DataFrame:

df = df.replace(np.nan, None)

This function is particularly useful when you need to export a pandas DataFrame to a database that uses None to represent missing values instead of NaN.

The following example shows how to use this syntax in practice.

Example: Replace NaN with None in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame({'A': [5, 6, 8, np.nan, 4, 15, 13],
                   'B': [np.nan, 12, np.nan, 10, 23, 6, 4],
                   'C': [2, 7, 6, 3, 2, 4, np.nan],
                   'D': [5, np.nan, 6, 15, 1, np.nan, 4]})

#view DataFrame
print(df)

      A     B    C     D
0   5.0   NaN  2.0   5.0
1   6.0  12.0  7.0   NaN
2   8.0   NaN  6.0   6.0
3   NaN  10.0  3.0  15.0
4   4.0  23.0  2.0   1.0
5  15.0   6.0  4.0   NaN
6  13.0   4.0  NaN   4.0

Notice that there are several NaN values throughout the DataFrame.

To replace each NaN value with None, we can use the following syntax:

#replace all NaN values with None
df = df.replace(np.nan, None)

#view updated DataFrame
print(df)

      A     B     C     D
0   5.0  None   2.0   5.0
1   6.0  12.0   7.0  None
2   8.0  None   6.0   6.0
3  None  10.0   3.0  15.0
4   4.0  23.0   2.0   1.0
5  15.0   6.0   4.0  None
6  13.0   4.0  None   4.0

Notice that each NaN in every column of the DataFrame has been replaced with None.

Note that if you’d like to only replace NaN values with None in one particular column, you can use the following syntax:

#replace NaN values with None in column 'B' only
df['B'] = df['B'].replace(np.nan, None)

#view updated DataFrame
print(df)

      A     B    C     D
0   5.0  None  2.0   5.0
1   6.0  12.0  7.0   NaN
2   8.0  None  6.0   6.0
3   NaN  10.0  3.0  15.0
4   4.0  23.0  2.0   1.0
5  15.0   6.0  4.0   NaN
6  13.0   4.0  NaN   4.0

Notice that the NaN values have been replaced with None in column ‘B’ only.

Related: How to Replace NaN Values with Zero in Pandas

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Replace Specific Values in Pandas
How to Filter a Pandas DataFrame by Column Values
How to Fill NA Values for Multiple Columns in Pandas

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *