【发布时间】:2017-09-05 23:48:16
【问题描述】:
我正在尝试做一些与this post 非常相似的事情。除非我有死亡的结果,例如1-6,我需要计算骰子所有可能值的条纹。
import numpy as np
import pandas as pd
data = [5,4,3,6,6,3,5,1,6,6]
df = pd.DataFrame(data, columns = ["Outcome"])
df.head(n=10)
def f(x):
x['c'] = (x['Outcome'] == 6).cumsum()
x['a'] = (x['c'] == 1).astype(int)
x['b'] = x.groupby( 'c' ).cumcount()
x['streak'] = x.groupby( 'c' ).cumcount() + x['a']
return x
df = df.groupby('Outcome', sort=False).apply(f)
print(df.head(n=10))
Outcome c a b streak
0 5 0 0 0 0
1 4 0 0 0 0
2 3 0 0 0 0
3 6 1 1 0 1
4 6 2 0 0 0
5 3 0 0 1 1
6 5 0 0 1 1
7 1 0 0 0 0
8 6 3 0 0 0
9 6 4 0 0 0
我的问题是“c”不正常。每次连胜时它都应该“重置”其计数器,否则 a 和 b 将不正确。
理想情况下,我想要像
这样优雅的东西def f(x):
x['streak'] = x.groupby( (x['stat'] != 0).cumsum()).cumcount() +
( (x['stat'] != 0).cumsum() == 0).astype(int)
return x
按照链接帖子中的建议。
【问题讨论】:
-
你能添加想要的输出吗?