How to Replace NaN with Blank/Empty Cells in Pandas

You can replace NaN with Blank/Empty cells using either fillna() or replace() in Python.

Single Column:

Method 1: df[‘Column_name’].fillna(' ')

Method 2: df[‘Column_name’].replace(np.nan,' ', regex=True)

Whole dataframe:

Method 1: df.fillna(' ')

Method 2: df.replace(np.nan, ' ', regex=True)


Example 1: single column

The following uses fillna() to replace NaN with empty cells in a single column.

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with Blank/Empty String
df['Col_1']=df['Col_1'].fillna(' ')

# print out the updated dataframe
print('Removed NaN in first column, replaced with empty cells: \n', df)

The updated dataframe has replaced NaN with Blank/Empty String in the first column.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  500.0    NaN

Removed NaN in first column, replaced with empty cells: 
   Col_1  Col_2
0   100    NaN
1         30.0
2   200  100.0
3         88.0
4   500    NaN

Example 2: whole dataframe

The following uses fillna() to replace NaN with empty cells in the whole dataframe.

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 300,500],
'Col_2': [np.nan, 30, 100, 88, 354,np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with Blank/Empty String
df=df.fillna(' ')

# updated dataframe with Blank/Empty String
print('Removed NaN, replaced with empty cells: \n', df)

The following shows the updated versions with empty cells, which originally were NaN.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  300.0  354.0
5  500.0    NaN

Removed NaN, replaced with empty cells: 
   Col_1 Col_2
0   100      
1          30
2   200   100
3          88
4   300   354
5   500      

Example 3: single column

The following shows using replace() to replace NaN with empty cells in a single column.

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with empty cells using replace()
df['Col_1']=df['Col_1'].replace(np.nan,' ',regex=True)

# print out the updated dataframe
print('Removed NaN in first column, replaced with empty cells: \n', df)

The following shows both versions, NaN and empty cells.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  500.0    NaN

Removed NaN in first column, replaced with empty cells: 
   Col_1  Col_2
0   100    NaN
1         30.0
2   200  100.0
3         88.0
4   500    NaN

Method 4: whole dataframe

The following shows using replace() to replace NaN with empty cells for the whole dataframe.

import pandas as pd
import numpy as np

# Create a dataframe with NaN
df = pd.DataFrame({'Col_1': [100, np.nan, 200, np.nan, 500],
'Col_2': [np.nan, 30, 100, 88, np.nan]})

# print out the dataframe with NaN
print('Dataframe with NaN: \n', df)

# replace NaN with empty cells using replace()
df=df.replace(np.nan, ' ',regex=True)

# print out the updated dataframe
print('Removed NaN, replaced with empty cells: \n', df)

The following is the output, which shows that all the NaN in the whole dataframe has been replaced with empty cells.

Dataframe with NaN: 
    Col_1  Col_2
0  100.0    NaN
1    NaN   30.0
2  200.0  100.0
3    NaN   88.0
4  500.0    NaN

Removed NaN, replaced with empty cells:
   Col_1 Col_2
0   100      
1          30
2   200   100
3          88
4   500      

Further Reading

The following shows two other tutorials related to NaN.