title: How to do nested groupby operations in a dataframe based on a condition on a column?
body: <p>I have to find the number of cycles within a column in my data frame (A cycle is defined when the variable goes from initial to some max value and again starts from some initial value). Whenever the variable has repeated values, I just average over them. In the desired data frame, I am appending the filter cycle number to that SNo as a suffix to know which cycle the given SNo is in. I need to get the min and the max for a given cycle and SNo (It is not predefined)</p>
  
  <p>An example of the data frame and the desired data frame are as follows: </p>
  
  <pre><code>SNo  VarPer Value
  1000    0   1.2
  1000    1   2.2
  1000    2   3.2
  1000    3   4.2
  1000    4   5.2
  1000    4   6.2
  1000    5   7.2
  1000    5   8.2
  1000    0   0.9
  1000    1   1.9
  1000    2   2.9
  1000    3   3.9
  1000    3   4.9
  1000    4   5.9
  1001    0   0.5
  1001    1   1.5
  1001    2   2.5
  1001    2   3.5
  1001    0   1
  1001    1   1
  1001    2   1
  </code></pre>
  
  <pre><code>SNo   VarPer Value
  1000_1  0   1.2
  1000_1  1   2.2
  1000_1  2   3.2
  1000_1  3   4.2
  1000_1  4   5.7
  1000_1  5   7.7
  1000_2  0   0.9
  1000_2  1   1.9
  1000_2  2   2.9
  1000_2  3   4.4
  1000_2  4   5.9
  1001_1  0   0.5
  1001_1  1   1.5
  1001_1  2   3
  1001_2  0   1
  1001_2  1   1
  1001_2  2   1
  </code></pre>
  
  <p>I have already tried the following:</p>
  
  <pre><code>y = dat.groupby(['SNo','VarPer'], as_index=False)['Value'].mean()
  </code></pre>
  
  <p>But this is grouping the entire thing without considering the cycles. I have about 70000 rows of data, so I need something that isn't terribly slow. Please help!</p>