elroi netzer
elroi netzer

Reputation: 11

Group by continuous indexes in Pandas DataFrame

I'm working on code for sensors data analysis using python.

I'm taking rows from DataFrame (of gyro data in the example) according to some condition.

import pandas as pd

gyro = pd.read_csv("gyroOutput.csv")
above = gyro[gyro['gyro_z'] > 0.30]

above
Out[162]: 
        gyro_x    gyro_y    gyro_z  elapsed
27    0.026632  0.021305  0.305731    4.927
28    0.017044  0.011718  0.344080    5.115
29    0.008522  0.013848  0.380299    5.289
30    0.006392  0.026632  0.412257    5.470
31    0.007457  0.005326  0.448476    5.643
32   -0.004261  0.012783  0.465521    5.822
33   -0.001065  0.000000  0.452737    6.002
34    0.009587  0.006392  0.445281    6.181
35    0.010653  0.001065  0.412257    6.361
36    0.006392  0.003196  0.373908    6.543
37   -0.006392  0.007457  0.320645    6.722
108  -0.036219  0.052198  0.323840   19.470
109  -0.061785 -0.001065  0.389887   19.654
110  -0.049002  0.018109  0.453803   19.835
111  -0.038350  0.078830  0.513458   20.015
112  -0.034088  0.011718  0.555003   20.192
113  -0.005326 -0.001065  0.607201   20.374
114   0.009587  0.058590  0.629571   20.553
115   0.038350 -0.029827  0.598679   20.727
116   0.006392  0.013848  0.546481   20.907
117   0.007457  0.030893  0.478304   21.086
118   0.012783 -0.035154  0.446346   21.266
119   0.005326 -0.026632  0.367516   21.444
352   0.007457  0.028762  0.313188   63.284
353   0.006392 -0.011718  0.332363   63.463
354   0.008522  0.030893  0.378169   63.643
355  -0.015979  0.039415  0.409062   63.822
356  -0.009587 -0.022371  0.423975   64.002
357  -0.008522  0.023436  0.450607   64.181
358  -0.011718  0.047937  0.453803   64.361

That result data frame (above) holds groups of continuous indexes rows. For example, lines 27-37.

I want to get all those group's, couldn't find any way to do it using DataFrame.groupby or any other function.

I could iterate over the rows and separate them myself, but maybe there's some simpler way using pandas functions.

Upvotes: 0

Views: 2293

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210912

IIUC:

In [294]: df.groupby(df.index.to_series().diff().ne(1).cumsum()).groups
Out[294]:
{1: Int64Index([27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37], dtype='int64'),
 2: Int64Index([108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119], dtype='int64'),
 3: Int64Index([352, 353, 354, 355, 356, 357, 358], dtype='int64')}

Upvotes: 6

Related Questions