title: Algorithm to know how often the event happened during some period of time within greater time range (Pandas)
body: <p>For example, I have Pandas dataset df of two columns: ['number'] and ['date']. In ['number'] column various numbers are placed, in ['date'] column date and time, when the number appeared, were placed in Epoch Unix Timestamp format.</p>
  <p>How can I count whether the particular number, for instance 20, appeared more than 15 times during time period of 100 seconds? (whole time range (df['date'][n] - df['date'][0] is much greater than 100 seconds)</p>
  <p>I wrote the Python code shown below. It works. But the problem is that if dataset is very big (millions of rows) and the event, I am looking for, occurs for example closer to the end of the dataset, it takes a lot of time to get the result. How can I make it work faster?</p>
  <pre><code>count = 0
  time = 0
  length = len(df['number'])
  for i in range(length):
      time = df['date'][i] + 100
      for j in range(1, length):
          if df['date'][j] &gt; time:
              count = df['number'][(df['date'] &gt;= df['date'][i]) &amp; (df['date'] &lt;= df['date'][j-1]) &amp; (df['number'] == 20)].count()
              break
          if j == length - 1:
              count = df['number'][(df['date'] &gt;= df['date'][i]) &amp; (df['date'] &lt;= df['date'][j]) &amp; (df['number'] == 20)].count()
              break
              
      if count &gt; 15:
          print('Number 20 appeared more than 15 times within the period of 100 seconds')
          break
  </code></pre>