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: ...)