0

I'm trying to set the mean value of group of products in my dataset (wants to iterate each category and fill the missing data eventually)

df.loc[df.iCode == 160610,'oPrice'].fillna(value=df[df.iCode == 160610].oPrice.mean(), inplace=True)

it's not working (maybe treating it like a copy)

Thanks

1 Answers1

0

df.loc[(df.iCode == 160610) & (df.oPrice.isna()),'oPrice'] = df.loc[df.iCode == 160610].oPrice.mean()

  • 1
    Mind adding some explanation of what you're doing here? – CBredlow Nov 22 '19 at 18:52
  • what he is doing is a form of chain indexing. First indexing `df.loc[df.iCode == 160610,'oPrice']` the next is `isna()`. by doing `df.loc[(df.iCode == 160610) & (df.oPrice.isna()),'oPrice']` allows you to index the dataframe only once. then explicitly assigning the values to the NAs `oPrice column` with the mean of the `oPrice column`. read https://stackoverflow.com/a/23296545/11724956 for more details. – Joshua Cabanas Nov 27 '19 at 14:13