I have a pd dataframe with multiple columns like so (simplified for ease of read) - each row consists of an id (uuid), index, and one or more features:
uuid index Atrium Ventricle
di-abc 0 20.73 26.21
di-abc 1 18.92 25.14
di-efg 7 19.02 0.30
di-efg 9 1.23 0.51
di-efg 6 21.24 26.02
di-hjk 3 22.10 25.16
di-hjk 6 19.16 25.57
I would like to:
- Find the outliers for each feature (i.e. columns 'Atrium' and 'Ventricle')
- Export the outliers in the following format:
outliers = {
'Atrium' : [
{'uuid' : 'di-efg', 'index' : 9, 'value' : 1.23},
],
'Ventricle' : [
{'uuid' : 'di-efg', 'index' : 7, 'value' : 0.30},
{'uuid' : 'di-efg', 'index' : 9, 'value' : 0.53},
]
}
Caveats (bonus points for handing this):
- The number of features (and hence columns) is dynamic
- A single row can contain zero, one, two, or more outliers
I am having difficulty with both steps outside of a double for loop. Is there an efficient way to compute the outliers from this dataframe?
Here is a working, though not efficient, method of capturing what I am trying to accomplish:
# initialize variables:
outliers = {}
features = ['Atrium', 'Ventricle']
# iterate over each feature:
for feature in features:
# set feature on outlier to empty list:
outliers[feature] = []
# create a dataframe of outliers for that specific feature:
outlier_df = df[df[feature] > (df[feature].mean() + df[feature].std())] # can mess with this if needed
outlier_df = outlier_df[['dicom', 'frame', 'index', feature]]
# iterate through the data frame and find the uuid, index, and feature:
for index, row in outlier_df.iterrows():
# append each outlier to the outlier dictionary:
outliers[feature].append({
'uuid' : row['uuid'],
'index' : row['index'],
'value' : row[feature],
})