Home > Mobile >  Pandas: how to normalize a column after groupby?
Pandas: how to normalize a column after groupby?

Time:10-15

I have a dataFrameGroupBy object with several columns, on of them is 'price'.

Since each group has a different price range, I would like to normalize each group separately.

following this question I tried :

grouped['priceNormed'] = grouped['price'].transform(lambda x: (x-x.mean()) / x.std())

but I get an error TypeError: 'DataFrameGroupBy' object does not support item assignment

The difference is (I think) that I am grouping by one column, but want to normalize another.

CodePudding user response:

You should assign back to the DataFrame, not GroupBy object:

grouped = df.groupby(...)
df['priceNormed'] = grouped['price'].transform(lambda x: (x-x.mean()) / x.std())
  • Related