python - Pandas - 使用 GroupBy 重置的累积和
问题描述
我有几千个时间序列的数据框。
- 每个时间序列由一个整数标识
对于每个时间序列,都有一个唯一的时间戳,因此我可以强制执行顺序。
- 对于下面的示例,我将时间戳替换为整数 idx
每个时间序列都有一个状态列
- 对于下面的示例,状态为 0 或 1
- 但我计算了一下,可以使用 NaN 或任何使它更容易的东西
在有一定数量的连续 1 之前,我需要“丢弃”所有行
- 它是 30,但对于下面的示例,我说 2,以保持示例简洁
所以,这里有一些示例数据......
test = pd.DataFrame({
'group': [1,1,1,1,1,1,1, 2,2,2,2,2,2,2],
'idx': [0,1,2,3,4,5,6, 0,1,2,3,4,5,6],
'value': [0,1,0,1,1,1,1, 0,1,1,1,0,1,0],
})
我想要的结果是...
desired_result = pd.DataFrame({
'group': [ 1,1,1, 2,2,2,2,2],
'idx': [ 4,5,6, 2,3,4,5,6],
'value': [ 1,1,1, 1,1,0,1,0],
})
我想我需要计算的是......
test = pd.DataFrame({
'group': [1,1,1,1,1,1,1, 2,2,2,2,2,2,2],
'idx': [0,1,2,3,4,5,6, 0,1,2,3,4,5,6],
'value': [0,1,0,1,1,1,1, 0,1,1,1,0,1,0],
#'consec':[0,1,0,1,2,3,4, 0,1,2,3,0,1,0], -- the cumulative sum of value, but resetting whenever a 0 is encountered
#'max_c': [0,1,1,1,2,3,4, 0,1,2,3,3,3,3], -- the cumulative max of consec
# ^ ^ ^ ^ ^ ^ ^ ^ -- rows I want to keep, as max_c >= 2
})
然后我可以把行放在哪里test[ test['max_c'] >= 2 ]
但是,我该如何计算consec
?
- 的累积和
value
,重置为 0,独立地由group
?
编辑:我最好的尝试,但感觉非常冗长......
test['cumsum'] = test.groupby(['group'])['value'].cumsum()
test['reset'] = test['cumsum'][ test.groupby(['group'])['value'].diff() == -1 ]
test['reset'] = test['reset'].fillna(0)
test['reset_cummax'] = test.groupby(['group'])['reset'].cummax()
test['consec'] = test['cumsum'] - test['reset_cummax']
test['c_max'] = test.groupby(['group'])['consec'].cummax()
解决方案
IIUC,您可以在列组上执行cumsum
a 之后groupby
,每次列值为eq
0 时,您也可以创建一个新组cumsum
。
test['consec'] = test.groupby(['group', test['value'].eq(0).cumsum()])['value'].cumsum()
test['max_c'] = test.groupby(['group'])['consec'].cummax()
print(test)
group idx value consec max_c
0 1 0 0 0 0
1 1 1 1 1 1
2 1 2 0 0 1
3 1 3 1 1 1
4 1 4 1 2 2
5 1 5 1 3 3
6 1 6 1 4 4
7 2 0 0 0 0
8 2 1 1 1 1
9 2 2 1 2 2
10 2 3 1 3 3
11 2 4 0 0 3
12 2 5 1 1 3
13 2 6 0 0 3
推荐阅读
- javascript - 吞咽用户参考。无法读取未定义的属性“替换”
- python - python中16b十六进制的数学运算
- javascript - 如何创建单元测试来验证此代码
- swift - 如何在 Swift 中说出字符串时在文本到语音中进行 1-5 秒的小停顿?
- java - 为正确的数学结果制定代码时遇到问题
- javascript - 无法使用 preventDefault() 以编程方式检查单选按钮
- python - 为什么屏幕不能以 Kv 语言加载?
- multithreading - 通过将 FnMut 包装成 Weak 将其发送到其他线程
> 抱怨缺少发送 - r - 如何在R中绘制多线图
- r - 手动留下一个交叉验证