Home > OS >  Slice consecutive rows pandas
Slice consecutive rows pandas

Time:10-14

Consider this df:

   col1     
0   3   
1   4 
2   11
3   12
4   13
5   23
6   25
7   26
8   27

Is there an easy and short way to slice/group the consecutive rows, without looping? Or is a loop necessary? I would like to achieve an output like this:

list_of_consc_rows = [(3,4),(11,12,13),(23),(25,26,27)]

CodePudding user response:

You can aggregate tuples by helper Series created by compare difference by Series.difffor not equal 1 and cumulative sum by Series.cumsum:

L = df.groupby(df['col1'].diff().ne(1).cumsum())['col1'].agg(tuple).tolist()
print (L)
[(3, 4), (11, 12, 13), (23,), (25, 26, 27)]
  • Related