我如何解决这个错误运行我的代码如下。我正在使用下面的函数并在运行窗口中实现循环,但最终得到以下错误。for 循环起作用并挂在某个点上。
def get_grps(s, thresh=-1, Nmin=3):
"""
Nmin : int > 0
Min number of consecutive values below threshold.
"""
m = np.logical_and.reduce([s.shift(-i).le(thresh) for i in range(Nmin)])
if Nmin > 1:
m = pd.Series(m, index=s.index).replace({False: np.NaN}).ffill(limit=Nmin - 1).fillna(False)
else:
m = pd.Series(m, index=s.index)
# Form consecutive groups
gps = m.ne(m.shift(1)).cumsum().where(m)
# Return None if no groups, else the aggregations
if gps.isnull().all():
return 0
else:
agg = s.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)
# agg2 = s2.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)
return agg, gps
data_spi = …Run Code Online (Sandbox Code Playgroud) 我有一个数组,我想选择前 2 个或范围,跳过下一个 2,选择下一个 2,然后继续直到列表末尾
list = [2, 4, 6, 7, 9,10, 13, 11, 12,2]
results_wanted = [2,4,9,10,12,2] # note how it skipping 2. 2 is used here as and example
Run Code Online (Sandbox Code Playgroud)
有没有办法在Python中实现这一点?
我有一个长度为 324 的数组。我试图根据数组中的值找到超过特定阈值的概率
我努力了::
data = [3,4, 5, 1, 5, 8, 9] ## sample
p = 100 * (4/(len(data)+1)) ## where 4 is my threshold.
Run Code Online (Sandbox Code Playgroud)
我不确定这是否正确,是否有更好的方法?