How can I replace NaN values with None in Pandas?

How can I replace NaN values with None in Pandas?

Pandas is a popular library in Python used for data manipulation and analysis. In many datasets, there are missing values represented as NaN (Not a Number). To handle such missing values, a common approach is to replace them with None, a default Python object representing missing data. This can be achieved in Pandas by using the .fillna() method and specifying the value to be replaced as None. This allows for easier handling of missing values in data analysis tasks.

Pandas: 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 DataFrameprint(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 DataFrameprint(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:

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

Cite this article

stats writer (2024). How can I replace NaN values with None in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-none-in-pandas/

stats writer. "How can I replace NaN values with None in Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-none-in-pandas/.

stats writer. "How can I replace NaN values with None in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-none-in-pandas/.

stats writer (2024) 'How can I replace NaN values with None in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-replace-nan-values-with-none-in-pandas/.

[1] stats writer, "How can I replace NaN values with None in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I replace NaN values with None in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top