Replacing Pandas or Numpy Nan with a None to use with MysqlDB
Last Updated :
28 Apr, 2025
The widely used relational database management system is known as MysqlDB. The MysqlDB doesn't understand and accept the value of 'Nan', thus there is a need to convert the 'Nan' value coming from Pandas or Numpy to 'None'. In this article, we will see how we can replace Pandas or Numpy 'Nan' with a 'None'.
Replacing NaN with None in Pandas
Example 1: The data frame, for which 'Nan' is to be replaced with 'None', is as follows:
The provided code uses the Pandas library to replace 'NaN' values in a DataFrame df with 'None'. It does so by using the replace() method with a dictionary mapping where keys (in this case, 'np.nan') are replaced by their corresponding values (in this case, 'None'). The resulting DataFrame 'replaced_df' contains 'None' in place of 'NaN' values.
Python3
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan,3], 'B': [np.nan, 5, 6], 'C': [7, 8, np.nan]})
replaced_df = df.replace({np.nan: None})
print(replaced_df)
Output:

Example 2: The data frame, for which 'Nan' is to be replaced with 'None', is as follows:

The provided code uses the Pandas library to replace 'NaN' values in a DataFrame df with 'None'. It does so by using the replace() method with a dictionary mapping where keys (in this case, 'np.nan') are replaced by their corresponding values (in this case, 'None'). The resulting DataFrame 'replaced_df' contains 'None' in place of 'NaN' values.
Python3
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [np.nan,6 ,3], 'B': [9, 5, np.nan], 'C': [np.nan, 8,5 ]})
print(df)
replaced_df = df.replace({np.nan: None})
print(replaced_df)
Output

Replacing NaN with None in NumPy
Example 1: The data frame, for which 'Nan' is to be replaced with 'None' , is as follows:
[ 1. nan 3. 6. 7.]
It creates a NumPy array named temp, replaces the np.nan values with None using np.where, and then prints the modified temp array.
Python3
import numpy as np
temp = np.array([1, np.nan, 3,6,7])
print(arr)
temp = np.where(np.isnan(temp), None, temp)
print(temp)
Output:
[1.0 None 3.0 6.0 7.0]
Example 2: The data frame, for which 'Nan' is to be replaced with 'None' , is as follows:
[ 4. 5. nan nan 7.]
It creates a NumPy array named temp, replaces the np.nan values with None using np.where, and then prints the modified temp array.
Python
import numpy as np
temp = np.array([1, np.nan, 3,6,7])
print(arr)
temp = np.where(np.isnan(temp), None, temp)
print(temp)
Output
[4.0 5.0 None None 7.0]
Similar Reads
Replace NaN with Blank or Empty String in Pandas? In this article, we will discuss how to replace NaN with Blank or Empty string in Pandas. Example: Input: "name": ['suraj', 'NaN', 'harsha', 'NaN'] Output: "name": ['sravan', , 'harsha', ' '] Explanation: Here, we replaced NaN with empty string.Replace NaN with Empty String using replace() We can re
2 min read
How to Replace Numpy NAN with String Dealing with missing or undefined data is a common challenge in data science and programming. In the realm of numerical computing in Python, the NumPy library is a powerhouse, offering versatile tools for handling arrays and matrices. However, when NaN (not a number) values appear in your data, you
2 min read
NumPy | Replace NaN values with average of columns Data visualization is one of the most important steps in machine learning and data analytics. Cleaning and arranging data is done by different algorithms. Sometimes in data sets, we get NaN (not a number) values that are unusable for data visualization. To solve this problem, one possible method is
5 min read
Replace all the NaN values with Zero's in a column of a Pandas dataframe Replacing the NaN or the null values in  a dataframe can be easily performed using a single line DataFrame.fillna() and DataFrame.replace() method. We will discuss these methods along with an example demonstrating how to use it.                            DataFrame.fillna()
3 min read
Replace NaN Values with Zeros in Pandas DataFrame NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to
5 min read