title: Fast subsetting in Pandas in Python
body: <p>I am running a loop a few million times, and I need to subset a different amount of data in each loop. I have a dataframe that has two columns, time (which is a time series) and electrode, which signifies a number between 1-64 for any electrode fired at that time. </p>
  
  <pre><code>time    electrode
   0          1
   1          43
   2          45
   3          12
   4          7
  </code></pre>
  
  <p>In each loop I need to subset the data, as such: </p>
  
  <pre><code>num_electrodes = []
  window_size = 5
  index = 0
  while index &lt; len(data['time']) - interval_size:
      start = data['time'][index]
      end = data['time'][index+window_size]
      window_data = data[(data['time'] &gt;= start) &amp; (data['time'] &lt; end)]
      num_electrodes.append(len(window_data['electrode'].unique()))
  </code></pre>
  
  <p>The really slow part of the code here is subsetting the dataframe and making a new dataframe, in the following code. </p>
  
  <pre><code>window_data = data[(data['time'] &gt;= start) &amp; (data['time'] &lt; end)]
  </code></pre>
  
  <p>Is there any good alternative to this?</p>