有没有办法使用 python pandas 对批量进行分组?

is there a way to groupby bulks using python pandas?

我有一个实验的不同试验的不同时间序列的 DataFrame。数据包含车辆加速状态的列。状态为 -1 表示减速,0 表示制动,1 表示加速。

我想对每个 -1、0、1 进行汇总,并且需要一种方法来将所有相邻的 1 分组,同时忽略其他 1。 0 和 -1 也一样。

例如: 这就是我的:

X轴加速度 X轴状态
0 0
0 0
0 0
0.87 1
0.88 1
0 0
-0.28 -1
-0.27 -1
0 0
0 0
0.46 1
0.23 1

这是我想要得到的:

平均X轴加速度 X轴状态 事件数
0 0 1
0.875 1 2
0 0 3
-0.275 -1 4
0 0 5
0.345 1 6

按列 X axis state 的连续值创建组并汇总 mean,最后按 list 更改列顺序:

g = df['X axis state'].ne(df['X axis state'].shift()).cumsum().rename('event number')

df = df.groupby([g, 'X axis state'])['X axis acceleration'].mean().reset_index()
df = df[['X axis acceleration','X axis state','event number']]

print (df)
   X axis acceleration  X axis state  event number
0                0.000             0             1
1                0.875             1             2
2                0.000             0             3
3               -0.275            -1             4
4                0.000             0             5
5                0.345             1             6

编辑:如果按移位值进行比较得到 Trues 如果值发生变化:

print (df['X axis state'].ne(df['X axis state'].shift()))
0      True
1     False
2     False
3      True
4     False
5      True
6      True
7     False
8      True
9     False
10     True
11    False
Name: X axis state, dtype: bool

所以如果加上累计和得到组:

print (df['X axis state'].ne(df['X axis state'].shift()).cumsum().rename('event number'))
0     1
1     1
2     1
3     2
4     2
5     3
6     4
7     4
8     5
9     5
10    6
11    6
Name: event number, dtype: int32