title: Python Pandas - Find and Group Outliers
body: <p>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:</p>
  <pre class="lang-py prettyprint-override"><code>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
  </code></pre>
  <p>I would like to:</p>
  <ol>
  <li>Find the outliers for each feature (i.e. columns 'Atrium' and 'Ventricle')</li>
  <li>Export the outliers in the following format:</li>
  </ol>
  <pre><code>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},
      ]
  }
  </code></pre>
  <p>Caveats (bonus points for handing this):</p>
  <ol>
  <li>The number of features (and hence columns) is dynamic</li>
  <li>A single row can contain zero, one, two, or more outliers</li>
  </ol>
  <p>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?</p>
  <p>Here is a working, though not efficient, method of capturing what I am trying to accomplish:</p>
  <pre class="lang-py prettyprint-override"><code># 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] &gt; (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],
           })
  </code></pre>