title: Python cumsum increment every time new value is encountered
body: <p>Coming from R, the code would be</p>
  
  <pre><code>x       &lt;- data.frame(vals = c(100,100,100,100,100,100,200,200,200,200,200,200,200,300,300,300,300,300))
  x$state &lt;- cumsum(c(1, diff(x$vals) != 0))
  </code></pre>
  
  <p>Which marks every time the difference between rows is non-zero, so that I can use it to spot transitions in data, like so:</p>
  
  <pre><code>   vals state
  1   100     1
  ...
  7   200     2
  ...
  14  300     3
  </code></pre>
  
  <p>What would be a clean equivalent in Python?</p>
  
  <p><strong>Additional question</strong></p>
  
  <p>The answer to the original question is posted below, but won't work properly for a grouped dataframe with pandas.</p>
  
  <p>Data here: <a href="https://pastebin.com/gEmPHAb7" rel="nofollow noreferrer">https://pastebin.com/gEmPHAb7</a>. Notice that there are 2 different filenames.</p>
  
  <p>When imported as <code>df_all</code> I group it with the following, and then apply solution posted below.</p>
  
  <pre><code>df_grouped = df_all.groupby("filename")
  df_all["state"] = (df_grouped['Fit'].diff() != 0).cumsum()
  </code></pre>