title: Count previous n rows where row meets condition
body: <p>How does one count the number of previous rows in a dataframe that contain the same value as a cell in that row?</p>
  
  <p>Given a dataframe, e.g.:</p>
  
  <pre class="lang-py prettyprint-override"><code>In [1]: df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo', 'foo', 'bar', 'baz', 'foo', 'foo']})
  
  In [2]: df1
  Out[2]:
  
      lkey
  0   foo
  1   bar
  2   baz
  3   foo
  4   foo
  5   bar
  6   baz
  7   foo
  8   foo
  
  </code></pre>
  
  <p>I would like to add a column which contains the number of times the value in <code>lkey</code> for that row appears in <code>lkey</code> in all previous rows of the dataframe.</p>
  
  <p>I have a dataframe of shape roughly 100000 x 15. My attempt at a for loop was useless <em>roll eyes</em>.</p>
  
  <p>The desired output would produce:</p>
  
  <pre class="lang-py prettyprint-override"><code>In [2]: df1['lkeyCount'] = (number of times lkey appears in previous rows in lkey column)
  Out[2]:
  
  lkey    lkeyCount
  0   foo 0
  1   bar 0
  2   baz 0
  3   foo 1
  4   foo 2
  5   bar 1
  6   baz 1
  7   foo 3
  8   foo 4
  </code></pre>
  
  <p>Thanks in advance!</p>