How to replace NaN with None in Pandas?

In Pandas, the NaN values can be replaced with None by using the pandas.DataFrame.fillna() method. This method takes a value, such as None, as an argument and will replace all occurrences of NaN in the DataFrame with the given value. This is useful for replacing missing data with a more meaningful value that can be used in downstream analysis. It can also be used to replace invalid values with a more meaningful value.


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:

x