Nan values in the Pandas dataframe are denoted using pd.Nat
, np.NaN
, None
.
You can replace nan with zero in a column of Pandas dataframe using the df.fillna(0, inplace=True)
statement.
Basic Example
df.fillna(0, inplace=True)
df
- Use the
inplace=True
parameter to fill in the same dataframe.
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 |
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.
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
Dataframe Will Look Like
The same dataframe object is modified, and the NaN
value is replaced with 0
.
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
The replace() method replaces the desired value with the specified value in the dataframe.
The main difference method between fillna()
and replace()
is that the
replace()
method can be used to replace any value with a specified value. Not only theNaN
values.fillna()
will only fill withNaN
values
Pass the inplace=True
to replace 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,
- Pass the subset of pandas dataframe columns as a list
- 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
Dataframe Will Look Like
The NaN
values in the specific columns are replaced with Zero.
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,
- Directly select the column using its name
- Invoke the
fillna()
method.
Use the inplace=True
parameter to replace in the same dataframe instead of creating a new dataframe object.
Code
df['Unit_Price'].fillna(0, inplace=True)
df
Dataframe Will Look Like
The NaN
values in the Unit_Price are replaced with zero.
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,
- 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.
- Use the
fillna()
method and pass the(df[‘column_name’].mean())
statement to replace the NaN value with the 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
Dataframe Will Look Like
The NaN value in the No_Of_Units is filled with the mean value of the column.
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 |