Replace NaN with Empty String Pandas
In data analysis, addressing missing values is crucial for accurate insights. When it comes to replacing NaN with an empty string in a Pandas DataFrame, various methods are available to ensure data consistency.
In this article, we will learn 4 different ways to replace NaN with an empty string in DataFrame.
- Replace using replace()
- Replace using apply()
- Replace using applymap()
- Replace using fillna()
- Conclusion
Table of Contents
1. Replace using replace()
The replace() method should be prefered way whenever you want to replace NaN with an empty string in DataFrame.
To replace NaN with an empty string, pass element to replace (here NaN) as first parameter and empty string as second parameter.
import pandas as pd
import numpy as np
# Creating a sample DataFrame with NaN values
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, np.nan, 35, np.nan],
'Score': [90, 85, np.nan, 92]}
df = pd.DataFrame(data)
# 👇 Replace NaN with empty string using replace()
df.replace(np.nan, '', inplace=True)
print(df)
Output:
Name Age Score 0 Alice 25 90 1 Bob 85 2 Charlie 35 3 David 92
In the above output you can see all the NaN values are replaced with an empty string.
2. Replace using apply()
The apply() method is used to apply a function to each element of a DataFrame. We can use this method to replace NaN with an empty string.
import pandas as pd
import numpy as np
# Creating a sample DataFrame with NaN values
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, np.nan, 35, np.nan],
'Score': [90, 85, np.nan, 92]}
df = pd.DataFrame(data)
# 👇 Replace NaN with empty string using apply()
df = df.apply(lambda x: x.replace(np.nan, ''))
print(df)
Output:
Name Age Score 0 Alice 25 90 1 Bob 85 2 Charlie 35 3 David 92
3. Replace using applymap()
The following example shows how to replace NaN with an empty string using applymap() method.
import pandas as pd
import numpy as np
# Creating a sample DataFrame with NaN values
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, np.nan, 35, np.nan],
'Score': [90, 85, np.nan, 92]}
df = pd.DataFrame(data)
# 👇 Replace NaN with empty string using applymap()
df = df.applymap(lambda x: '' if pd.isnull(x) else x)
print(df)
Output:
Name Age Score 0 Alice 25 90 1 Bob 85 2 Charlie 35 3 David 92
4. Replace using fillna()
The fillna() method can also be used to replace NaN with an empty string but not recommended.
import pandas as pd
import numpy as np
# Creating a sample DataFrame with NaN values
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, np.nan, 35, np.nan],
'Score': [90, 85, np.nan, 92]}
df = pd.DataFrame(data)
# 👇 Replace NaN with empty string using fillna()
df.fillna('', inplace=True)
print(df)
Output:
Name Age Score 0 Alice 25 90 1 Bob 85 2 Charlie 35 3 David 92
Conclusion
In this article, we looked at 4 different ways to replace NaN with an empty string in Pandas DataFrame.
It is recommended to use replace() method to replace NaN with an empty string.