Substitute Empty Cells by NaN in pandas DataFrame in Python (Example Code)

This article illustrates how to replace empty cells in a pandas DataFrame by NaNs in the Python programming language.

Preparing the Example

import pandas as pd                                          # Load pandas library
my_df = pd.DataFrame({'A':[5,' ', 5, '   ', 5, 5],           # Construct example DataFrame in Python
                      'B':['x','y', 'z', '', 'x', 'y']})
print(my_df)                                                 # Display example DataFrame in console
#      A  B
# 0    5  x
# 1       y
# 2    5  z
# 3        
# 4    5  x
# 5    5  y

Example: Replacing Empty Cells by NaN in pandas DataFrame in Python

my_df = my_df.replace(r'^s*$', float('NaN'), regex = True)  # Exchanging blanks by NaN
print(my_df)                                                 # Displaying updated DataFrame
#      A    B
# 0  5.0    x
# 1  NaN    y
# 2  5.0    z
# 3  NaN  NaN
# 4  5.0    x
# 5  5.0    y

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top