简体   繁体   中英

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM