2

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?

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
NatK
  • 21
  • 2
  • 1
    please post your code as a text instead of image/picture so that we can reproduce your dataframe – Anurag Dabas Apr 11 '21 at 06:05
  • 1
    Does this answer your question? [Pandas: filling missing values by mean in each group](https://stackoverflow.com/questions/19966018/pandas-filling-missing-values-by-mean-in-each-group) – Amit Vikram Singh Apr 11 '21 at 06:07

2 Answers2

1

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"))
Yefet
  • 2,010
  • 1
  • 10
  • 19
1

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))
Arturo Sbr
  • 5,567
  • 4
  • 38
  • 76