Replace NaN with Empty String using replace()

We can replace the NaN with an empty string using df.replace() function. This function will replace an empty string inplace of the NaN value.

Python3




# import pandas module
import pandas as pd
 
# import numpy module
import numpy as np
 
# create dataframe with 3 columns
data = pd.DataFrame({
 
    "name": ['sravan', np.nan, 'harsha', 'ramya'],
    "subjects": [np.nan, 'java', np.nan, 'html/php'],
    "marks": [98, np.nan, np.nan, np.nan]
})
 
# replace nan with empty string
# using replace() function
data.replace(np.nan, '')


Output:

 

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.

Similar Reads

Replace NaN with Empty String using replace()

We can replace the NaN with an empty string using df.replace() function. This function will replace an empty string inplace of the NaN value....

Replace NaN with Blank String using fillna()

...

Contact Us