Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Replace NaN with Blank or Empty String in Pandas?

  • Last Updated : 22 Nov, 2021

In this article, we will discuss how to replace NaN with Blank or Empty string in Pandas.

Creating dataframe with NaN value for demonstration

For this, we will create dataframe using pandas dataframe() object.

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]
})
  
# display
data

Output:

Method 1: Using replace() function

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

Syntax: dataframe.replace(np.nan, ”)

where

  • dataframe is the input dataframe
  • first parameter takes Nan value
  • second parameter replace the NaN with empty string

Example:

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:

Method 2: Using fillna()

This is used to replace multiple columns NaN values with an empty string.

Syntax: dataframe[[‘columns’ ]].fillna(”)

where

  • dataframe is the input dataframe
  • columns are the multiple columns in the dataframe

Example:

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 fillna() function
data[['name', 'subjects', 'marks']].fillna('')

Output:

we can also use fillna() directly without specifying columns:

dataframe.fillna('')

Example:

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 fillna() function
data.fillna('')

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!