Replace NaN with 0 Pandas
When conducting numerical operations, having 0 instead of NaN can be beneficial to ensure calculations proceed smoothly also in scenarios where NaN values should be represented visually, using 0 can help maintain continuity in plots and visualizations.
In this article, we will look at multiple ways to replace NaN values with 0 in Pandas DataFrame.
- Replace using fillna()
- Replace using replace()
- Replace using applymap()
- Replace using apply()
- Conclusion
Table of Contents
1. Replace using fillna()
The fillna() is a special function in Pandas that is used to replace NaN values with a specified value. It can be used to replace NaN values with 0 as well.
To replace NaN values with 0, apply fillna() on the DataFrame and pass value=0 parameter.
import pandas as pd
# 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 0 using fillna()
df.fillna(value=0, inplace=True)
print(df)
Output:
Name Age Score 0 Alice 25.0 90.0 1 Bob 0.0 85.0 2 Charlie 35.0 0.0 3 David 0.0 92.0
Note: Here inplace=True modifies the original DataFrame.
2. Replace using replace()
The replace() function is used to replace values in a DataFrame. It accept two arguments:
- to_replace - The value to be replaced
- value - The value to replace with
To replace NaN with 0 use replace(to_replace=np.nan, value=0, inplace=True)
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 0 using replace()
df.replace(to_replace=np.nan, value=0, inplace=True)
print(df)
Output:
Name Age Score 0 Alice 25.0 90.0 1 Bob 0.0 85.0 2 Charlie 35.0 0.0 3 David 0.0 92.0
3. Replace using applymap()
The following example uses applymap() to replace NaN values with 0. It applies a function to each element in dataframe.
Using this we can check and replace for each element in the DataFrame.
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 0 using applymap()
df = df.applymap(lambda x: 0 if pd.isnull(x) else x)
print(df)
Output:
Name Age Score 0 Alice 25.0 90.0 1 Bob 0.0 85.0 2 Charlie 35.0 0.0 3 David 0.0 92.0
4. Replace using apply()
The apply() function is used to apply a function along an axis of the DataFrame.
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 0 using apply()
df = df.apply(lambda x: [0 if pd.isnull(x[i]) else x[i] for i in range(len(x))])
print(df)
Output:
Name Age Score 0 Alice 25.0 90.0 1 Bob 0.0 85.0 2 Charlie 35.0 0.0 3 David 0.0 92.0
Conclusion
Replacing NaN with 0 in Pandas is a basic skill. Whether you opt for the fillna() method, the replace() method (prefer these 2 methods over other), the goal is to enhance the consistency and suitability of your data for analysis.