title: Pandas: Finding overlapping regions based on start- and stop coordinates
body: <p>I want to identify rows with start- and stop positions that overlap with the start- and stop position of other rows. There are a few restrictions that apply: </p>
  
  <ol>
  <li>The rows I want to check are a subset of the entire data set</li>
  <li>The rows in the subset should be compared to the entire data set</li>
  <li><s>The rows should not be compared with themselves</s></li>
  </ol>
  
  <p>Below is a minimal representation of the data set:</p>
  
  <pre><code>   id type  start  stop
  0   1   AP      0    10
  1   2   AP      3     7
  2   3   ES      5    15
  3   4   ES     12    18
  </code></pre>
  
  <p>Here's an image that describes the problem better. Each box represent an event/row, and the number represents their <code>ID</code>:
  <a href="https://i.stack.imgur.com/y7Mbe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y7Mbe.png" alt="Overlapping exons"></a></p>
  
  <p>And here's my desired output:</p>
  
  <pre><code>   id type  start  stop  number_of_overlapping_exons
  0   1   AP      0    10                            2
  1   2   AP      3     7                            2
  </code></pre>
  
  <p>I want to find the rows with <code>type</code> equal to AP that have other rows (of any type) that overlap their position. In the image above, the blue boxes represent AP events. There are two events/rows overlapping blue box 1 (boxes 2 and 3), so the <code>number_of_overlapping_exons</code> for <code>ID</code> 1 should be 2. Blue box 2 also has two overlapping events (boxes 1 and 3). Here's what I've got so far:</p>
  
  <pre><code>import pandas as pd
  
  # Sample input
  df = pd.DataFrame({
      "id": [1, 2, 3, 4],
      "type": ["AP", "AP", "ES", "ES"],
      "start": [0, 3, 5, 12],
      "stop": [10, 7, 15, 18]
  })
  
  # Extract only AP events
  ap = df.loc[df.type == "AP"]
  
  # Find events that overlap start positions in "ap"
  # by identifying "start" or "stop" positions in "df"
  # that are greater or equal to "start" positions in "ap".
  overlapping_start_positions = df.loc[(df.start &gt;= ap.start) | (df.stop &gt;= ap.start)]
  # Find events that overlap stop positions in "ap"
  # by identifying "start" or "stop" positions in "df"
  # that are smaller or equal to "stop" positions in "ap".
  overlapping_stop_positions = df.loc[(df.start &lt;= ap.stop) | (df.stop &lt;= ap.stop)]
  </code></pre>
  
  <p>I'm getting a <code>ValueError</code> when doing <code>overlapping_start_positions</code> saying</p>
  
  <pre><code>ValueError: Can only compare identically-labeled Series objects
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Come to think of it, condition 3:</p>
  
  <ol start="3">
  <li>The rows should not be compared with themselves</li>
  </ol>
  
  <p>is not really required. All events will overlap with themselves, so I can just subtract 1 from <code>number_of_overlapping_exons</code>.</p>