Aug-12-2020, 01:06 AM
Something like the following should work,
However, if the group (a set of records with the same
will yield to NaN value (since x.std()= 0 if x is an array consisting of only one element).
df['item_cnt_day'] = df.groupby('shop_id')['item_cnt_day'].transform(lambda x: (x-x.mean())/x.std())
However, if the group (a set of records with the same
shop_id
value) consist of one element only, this per-group scalingwill yield to NaN value (since x.std()= 0 if x is an array consisting of only one element).