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: ...)
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())))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With