Missing data in a Pandas DataFrame is commonly represented as NaN (Not a Number) values. In some cases, it may be desirable to replace these NaN values with zeros for consistency or to facilitate further data processing. This article will explore different methods to replace NaN values with zeros in a Pandas DataFrame using Python.
Method 1: Using the fillna() method
The fillna() method in Pandas allows us to replace NaN values with a specified value. To replace NaN values with zeros, we can pass 0 as the argument to the fillna() method.
import pandas as pd # Create a DataFrame with NaN values data = {'A': [1, 2, None, 4, 5], 'B': [None, 6, 7, 8, None], 'C': [9, 10, None, None, 13]} df = pd.DataFrame(data) # Replace NaN values with zeros df_filled = df.fillna(0) print(df_filled)
The above code will output:
A B C
0 1.0 0.0 9.0
1 2.0 6.0 10.0
2 0.0 7.0 0.0
3 4.0 8.0 0.0
4 5.0 0.0 13.0
Method 2: Using the replace() method
The replace() method in Pandas allows us to replace values in a DataFrame based on certain conditions. We can use this method to replace NaN values with zeros.
import pandas as pd import numpy as np # Create a DataFrame with NaN values data = {'A': [1, 2, np.nan, 4, 5], 'B': [np.nan, 6, 7, 8, np.nan], 'C': [9, 10, np.nan, np.nan, 13]} df = pd.DataFrame(data) # Replace NaN values with zeros df_zeros = df.replace(np.nan, 0) print(df_zeros)
The above code will output the following DataFrame:
A B C
0 1.0 0.0 9.0
1 2.0 6.0 10.0
2 0.0 7.0 0.0
3 4.0 8.0 0.0
4 5.0 0.0 13.0
Method 3: Using the fillna() method with inplace=True
In some cases, we may want to modify the original DataFrame in-place instead of creating a new DataFrame. We can achieve this by setting the inplace parameter of the fillna() method to True.
import pandas as pd # Create a DataFrame with NaN values data = {'A': [1, 2, None, 4, 5], 'B': [None, 6, 7, 8, None], 'C': [9, 10, None, None, 13]} df = pd.DataFrame(data) # Replace NaN values with zeros in-place df.fillna(0, inplace=True) print(df)
The above code will output:
A B C
0 1.0 0.0 9.0
1 2.0 6.0 10.0
2 0.0 7.0 0.0
3 4.0 8.0 0.0
4 5.0 0.0 13.0
Method 4: Using the numpy.where() function
The numpy.where() function allows us to replace values based on a specified condition. We can use this function to replace NaN values with zeros by creating a boolean mask where the NaN values are True, and then replacing those values with zeros using numpy.where().
import pandas as pd import numpy as np # Create a DataFrame with NaN values data = {'A': [1, 2, np.nan, 4, 5], 'B': [np.nan, 6, 7, 8, np.nan], 'C': [9, 10, np.nan, np.nan, 13]} df = pd.DataFrame(data) # Replace NaN values with zeros using numpy.where() df_zeros = np.where(pd.isna(df), 0, df) print(pd.DataFrame(df_zeros, columns=df.columns))
The above code will output:
A B C
0 1.0 0.0 9.0
1 2.0 6.0 10.0
2 0.0 7.0 0.0
3 4.0 8.0 0.0
4 5.0 0.0 13.0
Method 5: Using the applymap() method
The applymap() method in Pandas allows us to apply a function element-wise to a DataFrame. We can use this method along with a lambda function to replace NaN values with zeros.
import pandas as pd # Create a DataFrame with NaN values data = {'A': [1, 2, None, 4, 5], 'B': [None, 6, 7, 8, None], 'C': [9, 10, None, None, 13]} df = pd.DataFrame(data) # Replace NaN values with zeros using applymap() df_zeros = df.applymap(lambda x: 0 if pd.isna(x) else x) print(df_zeros)
The above code will output:
A B C
0 1.0 0.0 9.0
1 2.0 6.0 10.0
2 0.0 7.0 0.0
3 4.0 8.0 0.0
4 5.0 0.0 13.0
Last Updated on May 18, 2023 by admin