title: Sum pandas columns, excluding some rows based on other column values
body: <p>I'm attempting to determine the number of widget failures from a test population. </p>
  
  <p>Each widget can fail in 0, 1, or multiple ways. I'd like to calculate the number of failures of for each failure method, but once a widget is known to have failed, it should be excluded from future sums. In other words, the failure modes are known and ordered. If a widget fails via mode 1 and mode 3, I don't care about mode 3: I just want to count mode 1.</p>
  
  <p>I have a dataframe with one row per item, and one column per failure mode. If the widget fails in that mode, the column value is 1, else it is 0.</p>
  
  <pre><code>d = {"item_1": 
           {"failure_1":0, "failure_2":0}, 
       "item_2":
           {"failure_1":1, "failure_2":0}, 
       "item_3":
           {"failure_1":0, "failure_2":1}, 
       "item_4":
           {"failure_1":1, "failure_2":1}}
  
  df = pd.DataFrame(d).T
  display(df)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>        failure_1  failure_2
  item_1          0          0
  item_2          1          0
  item_3          0          1
  item_4          1          1
  </code></pre>
  
  <p>If I just want to sum the columns, that's easy: <code>df.sum()</code>. And if I want to calculate percentage failures, easy too: <code>df.sum()/len(df)</code>. But this counts widgets that fail in multiple ways, multiple times. For the problem stated, the best I can come up with is this:</p>
  
  <pre><code># create empty df to store results
  df2 = pd.DataFrame(columns=["total_failures"])
  
  for col in df.columns:
      # create a row, named after the column, and assign it the value of the sum
      df2.loc[col] = df[col].sum()
  
      # drop rows in the df column that are equal to 1
      df = df.loc[df[col] != 1]
  
  display(df2)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>          total_failures
  failure_1              2
  failure_2              1
  </code></pre>
  
  <p>This requires creating another dataframe (that's fine), but also requires iterating over the existing dataframe columns <em>and</em> deleting it a couple of rows at a time. If the dataframe takes a while to generate, or is needed for future calculations, this is not workable. I can deal with iterating over the columns.</p>
  
  <p>Is there a way to do this without deleting the original df, or making a temporary copy? (Not workable with large data sets.)</p>