Pandas dataframe might contain Nan values to denote the missing values.
You can replace nan with zero in a column of Pandas dataframe using the df.fillna(0, inplace=True)
statement.
Nan values are denoted using pd.Nat
, np.NaN
, None
.
This tutorial teaches you how to replace NaN
with zero in a column or replace NaN in all columns using the fillna()
or the replace()
method.
If You’re in Hurry…
You can use the fillna()
method to fill a value in place of the NaN
values. You can pass any value that needs to be replaced for NaN.
To make the fill operation in the same dataframe instance, use the inplace=True
parameter.
Code
df.fillna(0, inplace=True)
df
All the NaN values are replaced with Zeros.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | 0 | 01/05/2021 |
6 | Trackpad | 0.000 | 0.0 | 0 | 0 |
If You Want to Understand Details, Read on…
Different approaches are available to fill NaN
values with Zero or any other value.
To count the NaN values in the dataframe, read: How To Count Nan Values In Pandas Dataframe
To check if any value is Nan in the dataframe, read: – How to check if any value is NaN in a Pandas DataFrame
Table of Contents
Sample Dataframe
First, create a dataframe with missing values in it.
The sample dataframe contains missing values, and they are denoted using None
, np.nan
, and pd.NaT
.
Code
import pandas as pd
import numpy as np
data = {
"product_name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers","Trackpad"],
"Unit_Price":[500,200, 5000.235, 10000.550, 10000.550, 250.50,None],
"No_Of_Units":[5,5, 10, 20, 20, 8,np.nan],
"Available_Quantity":[5,6,10,"Not Available","Not Available", np.nan,np.nan],
"Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT]
}
df = pd.DataFrame(data)
df
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | NaN | 01/05/2021 |
6 | Trackpad | NaN | NaN | NaN | NaT |
Using FillNa
You can use the fillna() method to replace the NaN value with any other value.
- To fill the
NaN
value with Zero, pass0
to the method - To make the changes in the same dataframe, instead of creating a new copy, use the
inplace=True
parameter - If you invoke the method directly on the dataframe object, the
NaN
values in all the columns and rows will be replaced with the specified value
Code
The following code demonstrates how to replace the NaN
values with Zero in all columns and rows.
df.fillna(0, inplace=True)
df
The same dataframe object is modified, and the NaN
value is replaced with 0
.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | 0 | 01/05/2021 |
6 | Trackpad | 0.000 | 0.0 | 0 | 0 |
Using Replace
You can also use the replace() method to fill NaN
values with Zero.
*The main difference method between fillna()
and replace()
is that the replace()
method can be used to *replace any value* with some other value. Not only the NaN
values.*
It accepts two mandatory parameters.
- Value that needs to be replaced
- Value that should be used for replacing
You can pass the inplace=True
to make the replace operation in the same dataframe instead of creating a new dataframe.
Code
df.replace(np.NaN, 0, inplace=True)
df
The NaN
values are replaced with zero in the same dataframe object.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | 0 | 01/05/2021 |
6 | Trackpad | 0.000 | 0.0 | 0 | 0 |
Replace NaN With Zero in Mulitple Columns
To replace NaN
with Zero in multiple columns instead of the complete dataframe, you can pass the subset of pandas dataframe columns as a list and invoke the fillna()
method on specific columns.
It’ll replace the NaN
values in that specific columns.
Since it is in the subset of columns, you cannot use the inplace=True
parameter. Because of this, you need to assign the result to the same column list.
Code
The following code demonstrates how to replace NaN
with Zero in multiple columns.
df[['Unit_Price','No_Of_Units']] = df[['Unit_Price','No_Of_Units']].fillna(0)
df
The NaN
values in the specific columns are replaced with Zero.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | NaN | 01/05/2021 |
6 | Trackpad | 0.000 | 0.0 | NaN | NaT |
Replace NaN With Zero in a specific Column
To replace NaN
with zero in a specific column, you can directly select the column using its name and invoke the fillna()
method.
You can use the inplace=True
parameter to make the replace operation in the same dataframe instead of creating a new dataframe object.
Code
df['Unit_Price'].fillna(0, inplace=True)
df
The NaN
values in the Unit_Price are replaced with zero.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | NaN | 01/05/2021 |
6 | Trackpad | 0.000 | NaN | NaN | NaT |
Replace NaN With Zero in All Columns
To replace the NaN
with zero in all columns of the dataframe, you can invoke the fillna()
method directly in the dataframe object.
Use the inplace=True
parameter to replace NaN
s in the same dataframe instead of creating a new object.
Code
The following code demonstrates how to replace NaN
values with Zero in all columns of the dataframe.
df.fillna(0, inplace=True)
df
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.0 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.0 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.0 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.0 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.0 | 0 | 01/05/2021 |
6 | Trackpad | 0.000 | 0.0 | 0 | 0 |
Replace NaN With Mean
Sometimes, you may need to fill the NaN
values with the mean value of the same column.
You can use the fillna()
method and pass the (df[‘column_name’].mean())
statement to replace the NaN value with mean value of the column.
Code
The following code demonstrates how to fill Nan values of the No_Of_Units column with the mean value of the same column.
df['No_Of_Units'].fillna((df['No_Of_Units'].mean()), inplace=True)
df
The NaN value in the No_Of_Units is filled with the mean value of the column.
Dataframe Will Look Like
product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|
0 | Keyboard | 500.000 | 5.000000 | 5 | 11/5/2021 |
1 | Mouse | 200.000 | 5.000000 | 6 | 4/23/2021 |
2 | Monitor | 5000.235 | 10.000000 | 10 | 08/21/2021 |
3 | CPU | 10000.550 | 20.000000 | Not Available | 09/18/2021 |
4 | CPU | 10000.550 | 20.000000 | Not Available | 09/18/2021 |
5 | Speakers | 250.500 | 8.000000 | NaN | 01/05/2021 |
6 | Trackpad | NaN | 11.333333 | NaN | NaT |
Conclusion
You’ve learned how to replace nan
with zero in a column of Pandas dataframe. You’ve learned how to replace in a specific column, multiple columns at once and in all columns.
You also learned how to use the replace()
method to replace any other value with an alternate value and how to fill the NaN
value with the mean value.
If you’ve any questions, feel free to comment below.