title: How to insert a range of values inside a slice of pandas dataframe
body: <p>I have the following dataset:</p>
  
  <pre><code>df = pd.DataFrame({'timestamp': np.repeat(pd.date_range('2019-08-01', '2019-08-03'), 3),
                'group': ['A', 'B', 'C', 'B', 'B', 'C', 'B', 'C', 'C'],
                'id_appear': [np.nan, 1, np.nan, 1, 2, np.nan, 1, np.nan, np.nan]})
  </code></pre>
  
  <p>That looks like:</p>
  
  <pre><code>df.sort_values('group', inplace=True)
  
  
      timestamp   group   id_appear
  0   2019-08-01  A       NaN
  1   2019-08-01  B       NaN
  3   2019-08-02  B       NaN
  4   2019-08-02  B       NaN
  6   2019-08-03  B       NaN
  2   2019-08-01  C       NaN
  5   2019-08-02  C       NaN
  7   2019-08-03  C       NaN
  8   2019-08-03  C       NaN
  </code></pre>
  
  <p>But with nearly 6 million rows.</p>
  
  <p>I want to insert a series of numbers between 1 and N, where N represents the number of times that a group appears per day.</p>
  
  <p>I'm expecting the following: </p>
  
  <pre><code>timestamp       group   id_appear
  0   2019-08-01  A       1.0
  1   2019-08-01  B       1.0
  3   2019-08-02  B       1.0
  4   2019-08-02  B       2.0
  6   2019-08-03  B       1.0
  2   2019-08-01  C       1.0
  5   2019-08-02  C       1.0
  7   2019-08-03  C       1.0
  8   2019-08-03  C       2.0
  </code></pre>
  
  <p>I tried the following code:</p>
  
  <pre class="lang-py prettyprint-override"><code>indexes = df.index
  count = 1
  saved = None
  for pos, (index, row) in enumerate(df.iterrows()):
  
      if pos == 0 or ((row['group'] != saved['group']) or (row['timestamp'] != saved['timestamp'])):
          count = 1
      else:
          count +=1
      df.loc[index, 'id_appear'] = count
  
      saved = row
  </code></pre>
  
  <p>And, although it works, it is extremely inefficient. How could I improve the efficience of this code?</p>
  
  <p>Thank you!</p>