Python: how to fill up the mean value referencing from another dataframe column

Viewed 96

I have a housing dataframe:

enter image description here

where there are missing values in the Price column. I wish to fill the missing values by the mean price in the respective suburb.

This is my code for filling up the mean price by the same column:

all_housing_df['Price'].fillna(all_housing_df['Price'].mean())

How to fill in the mean price by the respective suburb?

2 Answers

You can use transform to fill missing values with the full list after grouping by Suburb

all_housing_df["Price"].fillna(all_housing_df.groupby("Suburb")["Price"].transform("mean"))

You can group by Suburb, get the mean Price and save this as a dictionary to conditionally replace null values.

# Create dictionary for NaN values
nan_dict = all_housing_df.groupby('Suburb')['Price'].mean().to_dict()

# Replace NaN with dictionary
all_housing_df['Price'].fillna(all_housing_df['Suburb'].map(nan_dict))
Related