I have a dataframe named df that has null values for Bandwidth_GB_Year.
I am trying to fill the null values based on the means that are grouped by another column, InternetService.
When I use fillna, it either doesn't fill the null values when i don't include inplace argument, or it removes all of the values from the column when I use inplace = true.
How do I fill the null values based on its matching internet service type?
In:
mean_values = df.groupby("InternetService")["Bandwidth_GB_Year"].mean().round(3)
print(mean_values)
Out:
InternetService
DSL 3717.539
Fiber Optic 3235.343
None 3224.141
Name: Bandwidth_GB_Year, dtype: float64
In:
df["Bandwidth_GB_Year"] = df["Bandwidth_GB_Year"].fillna(mean_values, inplace = True)
print(df["Bandwidth_GB_Year"])
Out:
1 None
2 None
3 None
4 None
5 None
...
9996 None
9997 None
9998 None
9999 None
10000 None
Name: Bandwidth_GB_Year, Length: 10000, dtype: object
The simplified example below shows how to replace NaN values with group mean values:
gives