I have a housing dataframe:
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?
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.