Search code examples
pythonpandasdataframedata-analysisnormalization

Pandas: how to normalize a column after groupby?


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.


Solution

  • 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())