title: Sum over all uniqe unordered combinations in a MultiIndex
body: <h2>Question</h2>
  <p>I want to find all <em>unique, unordered combinations</em> of the MultiIndex in which element 'A' is present and sum these rows.</p>
  <h2>Data</h2>
  <p>I have the following dataframe:</p>
  <pre class="lang-py prettyprint-override"><code>  df = pd.DataFrame({'col1': [2, 4, 6, 3], 'col2': [8, -2, 5, 3]}, 
                      index=pd.MultiIndex.from_tuples([('A', 'B'), ('B', 'A'), ('A', 'C'), ('C', 'A')]))
    df.index.names = ['from', 'to']
  </code></pre>
  <p>Output:</p>
  <pre><code>         col1  col2
  from to            
  A    B      2     8
  B    A      4    -2
  A    C      6     5
  C    A      3     3
  </code></pre>
  <h2>Expected Output</h2>
  <pre><code>         col1  col2
  from to            
  A    B      6     6
       C      9     8
  </code></pre>
  <p>The row line of the expected output is the sum of the first two rows. They are added, because you can change the order of the two elements so that they are identical <code>sorted(['A', 'B']) == sorted(['B', 'A'])</code>. Respectively, the second row of the output is the sum of the third and fourth row.</p>
  <h2>Ugly, unreadable code</h2>
  <p>My solution builds two new columns 'group1' and 'group2' which are ordered for the sole purpose to group the data. Yes, I could refactor my two functions into one lambda function to have fewer lines of code. However, I don't mind a few extra lines of code to use apply(). My problem is that the solution is almost unintelligible to any reader (this includes me in a few weeks).</p>
  <p>The code produces the expected output, but ... (to quote Raymond Hettinger) <strong>There Must Be a Better Way</strong>!</p>
  <pre class="lang-py prettyprint-override"><code>  def switch_cols(col1: str, col2: str, search_word):
        if col2 == search_word:
            return [col2, col1]
        return [col1, col2]
  
    def apply_switch_cols(df): 
        return switch_cols(df['from'], df['to'], search_word='A')
  
    df['group1'] = ''
    df['group2'] = ''
    df[['group1', 'group2']] = df.reset_index().apply(apply_switch_cols, axis=1).to_list()
    df = df.groupby(['group1', 'group2']).sum()
    df.index.names = ['from', 'to']
  </code></pre>