title: How to create a Pandas column to store the number of common elements between two filters of a data frame
body: <p>I have a Pandas data frame with relevant columns A, B, C. For context, there are several C's for every combination of A, B. I would like to compute the number of elements of C that each combination (a_i, b_j) of A, B has in common with the combination (a_0, b_j), where a_0 is fixed.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>A</th>
  <th>B</th>
  <th>C</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>a_0</td>
  <td>b_0</td>
  <td>c_0</td>
  </tr>
  <tr>
  <td>a_0</td>
  <td>b_0</td>
  <td>...</td>
  </tr>
  <tr>
  <td>a_0</td>
  <td>b_0</td>
  <td>c_m</td>
  </tr>
  <tr>
  <td>a_0</td>
  <td>b_1</td>
  <td>c_m+1</td>
  </tr>
  <tr>
  <td>a_0</td>
  <td>...</td>
  <td>...</td>
  </tr>
  <tr>
  <td>a_0</td>
  <td>b_n</td>
  <td>c_p</td>
  </tr>
  <tr>
  <td>...</td>
  <td>...</td>
  <td>...</td>
  </tr>
  <tr>
  <td>a_q</td>
  <td>b_n</td>
  <td>c_q</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have tried both using for-loops and pandas.Dataframe.apply, but both were taking too long due to the size of the data frame.</p>