Column to identify outliers rather than replace based on row values

Viewed 22

The data I'm working with data which has Weight and want to create an additional column that identifies the outliers.

I'm trying to add a column to a df which identifies outliers based on row level data, mean and std are created columns using .groupby as there are several subsets of data which need to be taken into account to create a representative mean & std for each group.

I've tried variations of:

df2["Weight_Outlier"] = df2["Weight"].apply(lambda x: True if x > (df2["Weight_Mean"] + df2["Weight_std"]*2.5) or x < (df2["Weight_Mean"] - df2["Weight_std"]*2.5) else False)

but keep getting 'The truth value of a series is ambiguous' Error and I just can't seem to resolve this to create a column either of 1/0 or True False which I can then .astype(int).

For anyone suggesting I should replace the outliers - yes I know but I am deliberately keeping them in for part of what I am doing to compare how training using sklearn.ensemble RandomForestClassifier is impacted. I want to look at the outliers also to see if outliers are 'successful'/'unsuccessful' in particular categories (subsets of data).

I therefore need to identify them in a column not replace them at this stage.

0 Answers
Related