finding outliers in subset of df

Viewed 25

below is example of df I use, sales data. df is big, having several Gb of data, few thousands brands, data for past 12 months, hundred of territories.

index    date brand  territory  value
0  2019-01-01     A      1     63
1  2019-02-01     A      1     91
2  2019-03-01     A      1    139
3  2019-04-01     A      1     80
4  2019-05-01     A      1    149

I want to find outliers for each individual brand across all territories for all dates

To find outliers within whole df I can use use outliers = df[(np.abs(stats.zscore(df['value'])) > 3)]

or stats.zscore(df['value'] just to calculate z-score

I would like to add column df[z-score] so I though about something like this but apparently it doesn't work

df['z-score'] = df.groupby('brand', as_index=False)['value'].stats.zscore(df['value'])

1 Answers

Use transform

df['z-score'] = df.groupby('brand')['value'].transform(stats.zscore)
Related