Use the following code to replace empty values with zeros in a complete pandas DataFrame, one column or multiple ones:
# entire DataFrame
your_df.replace([np.nan, pd.NA,'', None], 0)
# one column
your_df['your_col'].replace([np.nan, pd.NA,'', None], 0)
#multiple columns
your_df[['your_col1', 'your_coln']].replace([np.nan, pd.NA,'', None], 0)
Example DataFrame
We’ll import Data Analysis libraries pandas and numpy. Numpy will be used to generate a empty values in the Python lists that we will use in order to build your DataFrame.
import pandas as pd
import numpy as np
interviewed = [32, np.nan, 49, 40, np.nan]
hired = [6, pd.NA, 7, 5, 9]
dates = pd.date_range (start='1/29/2023', periods = 5, freq = 'B' )
We will now construct our DataFrame:
hiring = pd.DataFrame (dict (dates = dates, interviewed = interviewed, hired = hired))
And look into its values:
hiring.head()
Here is the data:
dates | interviewed | hired | |
---|---|---|---|
0 | 2023-01-30 | 32.0 | 6 |
1 | 2023-01-31 | NaN | <NA> |
2 | 2023-02-01 | 49.0 | 7 |
3 | 2023-02-02 | 40.0 | 5 |
4 | 2023-02-03 | NaN | 9 |
Replace empty nan values with zero in DataFrame columns
We will use the following code to change all empty values to zero. Note that we pass a list of possible empty values to the replace method.
hiring = hiring.replace([np.nan, pd.NA,'', None], 0)
hiring.head()
Here’s our DataFrame:
dates | interviewed | hired | |
---|---|---|---|
0 | 2023-01-30 | 32.0 | 6 |
1 | 2023-01-31 | 0.0 | 0 |
2 | 2023-02-01 | 49.0 | 7 |
3 | 2023-02-02 | 40.0 | 5 |
4 | 2023-02-03 | 0.0 | 9 |
Change nan to 0 in a specific pandas column
We are able also to selectively modify empty values in one or multiple columns of our DataFrame:
hiring['interviewed'].replace([np.nan, pd.NA,'', None], 0)
This will return the following series:
0 32.0 1 0.0 2 49.0 3 40.0 4 0.0 Name: interviewed, dtype: float64
Modify nans in multiple DataFrame columns
In the same way we can replace empty values in several columns:
hiring[['interviewed','hired']].replace([np.nan, pd.NA,'', None], 0)
Replace empty values by a string
In this case we’ll swap the empty values with the string value None
hiring[['interviewed','hired']].replace([np.nan, pd.NA,'', None], 'None')