title: pandas inclusive unique values from two columns
body: <p>I can't find any elegant way to select unique rows from column <code>A</code> and column <code>B</code> but not jointly and not in a sequence. This is in order to keep "inclusive" intersection of unique values from these two columns.</p>
  
  <p>My aim is to keep as many unique values as possible across columns <code>A</code> and <code>B</code>. The columns are considered jointly but I am looking for all the unique "combinations" of their values...</p>
  
  <p>Sample dataframe</p>
  
  <pre><code>df1 = pd.DataFrame({"A": [ "A1", "A2", "A2", "A3", "A3", ],
                      "B": [ "B1", "B1", "B2", "B3", "B1", ], },
                     index=[ 0, 1, 2, 3, 4, ])
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>    A   B
  0  A1  B1
  1  A2  B1
  2  A2  B2
  3  A3  B3
  4  A3  B1
  </code></pre>
  
  <p>This does nothing useful...</p>
  
  <pre><code>df2 = df1.drop_duplicates( subset=[ "A", "B", ], keep="first", inplace=False, )
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>    A   B
  0  A1  B1
  1  A2  B1
  2  A2  B2
  3  A3  B3
  4  A3  B1
  </code></pre>
  
  <p>The below code leaves duplicated <code>B1</code> which can be removed later using <code>drop_duplicates</code> on column <code>B</code> but then <code>A2</code> will also be removed and it could have been kept if it was present in one row with <code>B2</code>, as it was at the <code>index=2</code> of the original dataframe.</p>
  
  <pre><code>df3 = df1.drop_duplicates( subset=[ "A", ], keep="first", inplace=False, )
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>    A   B
  0  A1  B1
  1  A2  B1
  3  A3  B3
  </code></pre>
  
  <p>As mentioned above <code>A2</code> is removed, but there was an option to keep it, if it was present in one row with <code>B2</code>, as it was at the <code>index=2</code> of the original dataframe.</p>
  
  <pre><code>df4 = df3.drop_duplicates( subset=[ "B", ], keep="first", inplace=False, )
  
      A   B
  0  A1  B1
  3  A3  B3
  </code></pre>
  
  <p><strong>Desired result:</strong></p>
  
  <pre><code>    A   B
  0  A1  B1
  1  A2  B2
  2  A3  B3
  </code></pre>
  
  <p>So my aim is to keep as many unique values as possible across columns <code>A</code> and <code>B</code>. The columns are considered jointly but I am looking for all the unique "combinations" of their values...</p>