Skip to main content

Command Palette

Search for a command to run...

Changing all NaN values in a DataFrame to an empty string ('')

Published
1 min read
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

import pandas as pd
import numpy as np

# Example DataFrame with NaN values
df = pd.DataFrame({'Column1': [1, np.nan, 3], 'Column2': ['A', np.nan, 'C'], 'Column3': [np.nan, np.nan, np.nan]})

# Replace NaN values with empty string
df.fillna('', inplace=True)

print(df)

output: