统计连续满足条件的值的数量(Pandas数据框架)

9
所以我两天前创建了这个与我的问题相关的帖子,并得到了答案。
我的数据由20行和2500列组成。每一列都是唯一的产品,行是时间序列,是测量结果。因此,每个产品都会被测量20次,共有2500个产品。
这次我想知道我的测量结果可以连续保持在特定阈值以上的行数。也就是说,我想计算连续的值中有多少个大于某个值,比如5。
例如:A = [1, 2, 6, 8, 7, 3, 2, 3, 6, 10, 2, 1, 0, 2] 我们按照上述定义,应该得到NumofConsFeature = 3作为结果(如果有多个系列符合条件,则取最大值)。
我想通过.gt进行过滤,然后获取索引,随后使用循环来检测连续的索引号码,但没有成功。
在第二阶段中,我想知道连续系列的第一个值的索引。对于上面的例子,那将是3。 但我不知道如何做到这一点。
谢谢你的帮助。

请考虑接受您之前发布的帖子上的答案 - 您可以通过点击答案旁边的复选标记来执行此操作。 - andrew_reece
“6, 8, 7” 应该改为“6, 7, 8”吗? - andrew_reece
你的意思是第一个值的索引应该是2而不是3吗?对于A=[1,2,6,7,8...],6的索引是2。 - andrew_reece
从0开始,你是正确的,是的应该是2。而且,6, 8, 7没有理由按从小到大的顺序排列。 - meliksahturker
我明白了。谢谢你的澄清。 - andrew_reece
显示剩余4条评论
5个回答

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

这提供了

    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

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


3

有一种简单的方法可以做到这一点。
假设你的列表是这样的:A=[1,2,6,8,7,6,8,3,2,3,6,10,6,7,8,2,1,0,2]
如果你想找出连续的系列中大于6的值的数量和长度为5。例如,在这里,你的答案是2。有两个系列的值大于6且序列的长度为5。在python和pandas中,我们可以这样做:

 condition = (df.wanted_row > 6) & \
            (df.wanted_row.shift(-1) > 6) & \
            (df.wanted_row.shift(-2) > 6) & \
            (df.wanted_row.shift(-3) > 6) & \
            (df.wanted_row.shift(-4) > 6)

consecutive_count = df[condition].count().head(1)[0]

这里,我用漂亮的语法写成:condition = eval(' & '.join([f'(pos.shift({x})>6)' for x in range(num_consecutive)])) = 1 - Landmaster

0
你可以在你的Series上应用diff(),然后只需计算连续条目的数量,其中差值为1且实际值高于你的截止值。最大计数是连续值的最大数量。
首先计算diff()
df = pd.DataFrame({"a":[1, 2, 6, 7, 8, 3, 2, 3, 6, 10, 2, 1, 0, 2]})
df['b'] = df.a.diff()

df
     a    b
0    1  NaN
1    2  1.0
2    6  4.0
3    7  1.0
4    8  1.0
5    3 -5.0
6    2 -1.0
7    3  1.0
8    6  3.0
9   10  4.0
10   2 -8.0
11   1 -1.0
12   0 -1.0
13   2  2.0

现在计算连续序列:

above = 5
n_consec = 1
max_n_consec = 1

for a, b in df.values[1:]:
    if (a > above) & (b == 1):
        n_consec += 1
    else: # check for new max, then start again from 1
        max_n_consec = max(n_consec, max_n_consec)
        n_consec = 1

max_n_consec
3

感谢您的回复,但是我有非常多的列,而且随着独特产品的增加,它们将不断增加。因此,我无法为每个系列/产品编写代码。所以我需要一个更通用的解决方案,适用于无限数量的列。 - meliksahturker

0

这里有一个带有maxisland_start_len_mask的例子 -

# https://dev59.com/yVQJ5IYBdhLWcg3w963x#52718782/ @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)

示例运行 -

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])

非常感谢您的精彩回复。虽然我有些难以理解,但它在一个示例数组a上运行得很好。 然而,当我尝试使用数据时,它给了我一个错误,内容如下: File "<string>", line 74, in maxisland_start_len IndexError: index 126 out-of-bounds in maximum.reduceat [0, 126) 而第74行是 max_island_percol_len = np.maximum.reduceat(island_lens, grp_shift_idx[:-1]) 我是新手,所以语法对我来说有点陌生。 我正在努力理解,但没有注释,这并不容易。您能否详细说明一下?非常感谢。 - meliksahturker
@crinix 输入是 NumPy 数组还是其他什么? - Divakar
它最初是一个 Pandas DataFrame,但是通过 na = df.values 被转换成了 Numpy 数组。 - meliksahturker
我甚至尝试将整个表格除以10,并将阈值设置为1。结果的错误与在将数据除以10之前阈值为10时相同:IndexError: index 126 out-of-bounds in maximum.reduceat [0, 126) - meliksahturker
@Divakar 我删除了三分之一的列(数据),它可以正常运行而不会出错,但是计算结果错误。非常感谢您的帮助,但我很难理解和使用您的方法。所以,您能否至少帮我添加每行代码的注释,以便我可以诊断并改进它,使其正常工作?提前致谢。 - meliksahturker
显示剩余8条评论

0

这是我使用numpy的方法:

import pandas as pd
import numpy as np


df = pd.DataFrame({"a":[1, 2, 6, 7, 8, 3, 2, 3, 6, 10, 2, 1, 0, 2]})


consecutive_steps = 2
marginal_price = 5

assertions = [(df.loc[:, "a"].shift(-i) < marginal_price) for i in range(consecutive_steps)]
condition = np.all(assertions, axis=0)

consecutive_count = df.loc[condition, :].count()
print(consecutive_count)

这将产生6


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接