Introduction
In this tutorial, we want to replace null values in a Pandas DataFrame. In order to do this, we use the the fillna() method of Pandas.
Import Libraries
First, we import the following python modules:
import numpy as np
import pandas as pd
Create Pandas DataFrame
Next, we create a Pandas DataFrame with some example data from a dictionary:
mydict = {
"language": ["Python", np.nan, "Python", "Java"],
"framework": ["FastAPI", np.nan, "Django", np.nan],
"users": [np.nan, 7000, 20000, np.nan],
}
df = pd.DataFrame(mydict)
df
Replace Missing Values with Constant Values
Now, we would like to replace all null values of the DataFrame with constant values.
The null values of the columns "language" and "framework" should be replaced with the value "unknown". The null values of the column "users" should be replaced with the value 0.
To do this, we use fillna() method of Pandas and pass a dictionary with the new values as argument:
new_values = {
"language": "unknown",
"framework": "unknown",
"users": 0
}
df_cleaned = df.fillna(value=new_values)
df_cleaned
Replace Missing Values with Aggregated Values
Next, we would like to replace null values of the DataFrame with aggregated values.
The null values of the column "users" should be replaced with the mean of the column values.
To do this, we use the mean() method of Pandas for calculating the mean of the column and the fillna() method of Pandas for replacing the null values with the mean:
users_mean = df['users'].mean()
df['users'] = df['users'].fillna(value=users_mean)
df
Conclusion
Congratulations! Now you are one step closer to become an AI Expert. You have seen that it is very easy to replace null values in a Pandas DataFrame. We can simply use the fillna() method of Pandas. Try it yourself!
Also check out our Instagram page. We appreciate your like or comment. Feel free to share this post with your friends.