World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
I am not able to replace Null values with mean, median, mode. Now left with with arbitary value . please give me hint how to do that
Basically arbitary value is when you try to replace one value with another like null to others
To replace null values with the mean, median, or mode of a column, you can use the following approaches:
Mean:
You can use the mean()
function from Pandas to calculate the mean of a column. Then you can use the fillna()
function to replace null values with the mean. Here's an example:
import pandas as pd
# Load the data
df = pd.read_csv("data.csv")
# Calculate the mean of the column
mean = df["column_name"].mean()
# Replace null values with the mean
df["column_name"].fillna(mean, inplace=True)
Median:
You can use the median()
function from Pandas to calculate the median of a column. Then you can use the fillna()
function to replace null values with the median. Here's an example:
import pandas as pd
# Load the data
df = pd.read_csv("data.csv")
# Calculate the median of the column
median = df["column_name"].median()
# Replace null values with the median
df["column_name"].fillna(median, inplace=True)
Mode:
You can use the mode()
function from Pandas to calculate the mode of a column. Then you can use the fillna()
function to replace null values with the mode. Here's an example:
import pandas as pd
# Load the data
df = pd.read_csv("data.csv")
# Calculate the mode of the column
mode = df["column_name"].mode()
# Replace null values with the mode
df["column_name"].fillna(mode, inplace=True)
If you want to use an arbitrary value to replace the null values, you can use the fillna()
function as shown in the examples above and pass the arbitrary value as an argument to the function.