在 Pandas DataFrame 中按连续索引分组 [英] Group by continuous indexes in Pandas DataFrame

查看:124
本文介绍了在 Pandas DataFrame 中按连续索引分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 编写用于传感器数据分析的代码.

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

我正在根据某些条件从 DataFrame(示例中的陀螺仪数据)中获取行.

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

该结果数据框(上图)包含连续索引行的组.例如,第 27-37 行.

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

我想获取所有这些组,但无法使用 DataFrame.groupby 或任何其他函数找到任何方法.

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

我可以遍历行并自己将它们分开,但也许有一些使用 Pandas 函数的更简单的方法.

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

推荐答案

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')}

这篇关于在 Pandas DataFrame 中按连续索引分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆