Pandas normalise by column on groupby

Viewed 226

Given a pandas dataframe such as

import pandas as pd

df = pd.DataFrame({'id': ['id1','id1','id2','id2'] , 
                   'x':  [1,2,3,4], 
                   'y':  [10,20,30,40]})

each numerical column may be normalised to the unit interval [0,1] with

columns = ['x', 'y']

for column in columns:
    df[column] = (df[column] - df[column].min()) / (df[column].max() - df[column].min())

resulting in

    id         x         y
0  id1  0.000000  0.000000
1  id1  0.333333  0.333333
2  id2  0.666667  0.666667
3  id2  1.000000  1.000000

However, how to apply this normalisation on each numerical column for each id? The expected outcome would be in this oversimplified example

    id         x         y
0  id1  0.000000  0.000000
1  id1  1.000000  1.000000
2  id2  0.000000  0.000000
3  id2  1.000000  1.000000

It proves unclear how to update each normalised column after

df.groupby(['id']).apply(lambda x: ...)
3 Answers

Use GroupBy.transform:

columns = ['x', 'y']
g = df.groupby('id')[columns]
df[columns] = (df[columns] - g.transform('min')) / (g.transform('max') - g.transform('min'))
    
print (df)
    id    x    y
0  id1  0.0  0.0
1  id1  1.0  1.0
2  id2  0.0  0.0
3  id2  1.0  1.0

It proves unclear how to update each normalised column after df.groupby(['id']).apply(lambda x: ...)

You can apply again:

df.groupby(["id"])\
.apply(lambda id_df: id_df[columns]\
                     .apply(lambda serie: (serie - serie.min()) / (serie.max() - serie.min())))

Probably not the best way, but if your dataframe is not huge, then this will do:

for column in columns:
    for id in list_of_IDs:
        df.loc[df.loc[id] == i,column] = (df.loc[df.loc[id] == i,column] - df.loc[df.loc[id] == i,column].min()) / df.loc[df.loc[id] == i,column].max() - df.loc[df.loc[id] == i,column].min())
Related