计算满足条件的连续值的数量(Pandas Dataframe)

cri*_*nix 7 python numpy series dataframe pandas

所以我在2 天前创建了这篇关于我的问题的帖子,谢天谢地得到了答案。

我有一个由 20 行和 2500 列组成的数据。每一列都是一个独特的产品,行是时间序列,测量结果。因此每个产品测量 20 次,就有 2500 个产品。

这次我想知道有多少连续行我的测量结果可以保持在特定阈值之上。又名:我想计算高于某个值的连续值的数量,假设为 5。

A = [1, 2, 6 , 8 , 7 , 3, 2, 3, 6 , 10 , 2, 1, 0, 2] 我们以粗体显示这些值,根据我上面的定义,我应该得到 NumofConsFeature = 3 作为结果。(如果满足条件的系列超过1个,则取最大值)

我想过使用 .gt 进行过滤,然后获取索引并在之后使用循环来检测连续的索引号,但无法使其工作。

在第二阶段,我想知道连续系列的第一个值的索引。对于上面的示例,这将是 3。但我不知道如何实现这一点。

提前致谢。

小智 6

这是仅使用 Pandas 函数的另一个答案:

A = [1, 2, 6, 8, 7, 3, 2, 3, 6, 10, 2, 1, 0, 2]
a = pd.DataFrame(A, columns = ['foo'])
a['is_large'] = (a.foo > 5)
a['crossing'] = (a.is_large != a.is_large.shift()).cumsum()
a['count'] = a.groupby(['is_large', 'crossing']).cumcount(ascending=False) + 1
a.loc[a.is_large == False, 'count'] = 0
Run Code Online (Sandbox Code Playgroud)

这使

    foo  is_large  crossing  count
0     1     False         1      0
1     2     False         1      0
2     6      True         2      3
3     8      True         2      2
4     7      True         2      1
5     3     False         3      0
6     2     False         3      0
7     3     False         3      0
8     6      True         4      2
9    10      True         4      1
10    2     False         5      0
11    1     False         5      0
12    0     False         5      0
13    2     False         5      0
Run Code Online (Sandbox Code Playgroud)

从那里您可以轻松找到最大值及其索引。


Div*_*kar 0

这是一个maxisland_start_len_mask-

# /sf/answers/3690314771/ @Divakar
def maxisland_start_len_mask(a, fillna_index = -1, fillna_len = 0):
    # a is a boolean array

    pad = np.zeros(a.shape[1],dtype=bool)
    mask = np.vstack((pad, a, pad))

    mask_step = mask[1:] != mask[:-1]
    idx = np.flatnonzero(mask_step.T)
    island_starts = idx[::2]
    island_lens = idx[1::2] - idx[::2]
    n_islands_percol = mask_step.sum(0)//2

    bins = np.repeat(np.arange(a.shape[1]),n_islands_percol)
    scale = island_lens.max()+1

    scaled_idx = np.argsort(scale*bins + island_lens)
    grp_shift_idx = np.r_[0,n_islands_percol.cumsum()]
    max_island_starts = island_starts[scaled_idx[grp_shift_idx[1:]-1]]

    max_island_percol_start = max_island_starts%(a.shape[0]+1)

    valid = n_islands_percol!=0
    cut_idx = grp_shift_idx[:-1][valid]
    max_island_percol_len = np.maximum.reduceat(island_lens, cut_idx)

    out_len = np.full(a.shape[1], fillna_len, dtype=int)
    out_len[valid] = max_island_percol_len
    out_index = np.where(valid,max_island_percol_start,fillna_index)
    return out_index, out_len

def maxisland_start_len(a, trigger_val, comp_func=np.greater):
    # a is 2D array as the data
    mask = comp_func(a,trigger_val)
    return maxisland_start_len_mask(mask, fillna_index = -1, fillna_len = 0)
Run Code Online (Sandbox Code Playgroud)

样本运行 -

In [169]: a
Out[169]: 
array([[ 1,  0,  3],
       [ 2,  7,  3],
       [ 6,  8,  4],
       [ 8,  6,  8],
       [ 7,  1,  6],
       [ 3,  7,  8],
       [ 2,  5,  8],
       [ 3,  3,  0],
       [ 6,  5,  0],
       [10,  3,  8],
       [ 2,  3,  3],
       [ 1,  7,  0],
       [ 0,  0,  4],
       [ 2,  3,  2]])

# Per column results
In [170]: row_index, length = maxisland_start_len(a, 5)

In [172]: row_index
Out[172]: array([2, 1, 3])

In [173]: length
Out[173]: array([3, 3, 4])
Run Code Online (Sandbox Code Playgroud)