Pandas:在一定条件下对前N个元素求和

2024-03-29 11:05:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,看起来像:

date                      condition        count        Value 
01,01,2018 08:00             A               1            0
01,01,2018 08:01             A               2            1
01,01,2018 08:02             A               3            4
01,01,2018 08:03             B               1            2
01,01,2018 08:04             B               2            1
01,01,2018 08:05             B               3            7
01,01,2018 08:06             B               4            0
01,01,2018 08:07             C               1            11
01,01,2018 08:08             C               2            2
01,01,2018 08:09             C               3            0
01,01,2018 08:10             C               4            0
01,01,2018 08:11             C               5            0
01,01,2018 08:12             A               1            3
01,01,2018 08:13             A               2            1
01,01,2018 08:14             B               1            0
01,01,2018 08:15             B               2            0
01,01,2018 08:16             B               3            0
01,01,2018 08:17             C               1            8

我试图检查在特定条件下,这些值的总和是否等于0:

如果条件=B,则必须在从计数=1到计数=3的间隔内进行求和。 然后,如果该间隔的值之和=0,则另一列error的值应为1。你知道吗

在本例中,从08:03到08:05,sum=10;从08:14到08:16,sum=0。因此,在这种情况下,除了08:14到08:16之外,列error中的值应该都是=0,其中它们应该是=1。你知道吗

获取:

  date                      condition        count        Value     error
    01,01,2018 08:00             A               1            0         0
    01,01,2018 08:01             A               2            1         0
    01,01,2018 08:02             A               3            4         0
    01,01,2018 08:03             B               1            2         0
    01,01,2018 08:04             B               2            1         0
    01,01,2018 08:05             B               3            7         0
    01,01,2018 08:06             B               4            0         0
    01,01,2018 08:07             C               1            11        0
    01,01,2018 08:08             C               2            2         0
    01,01,2018 08:09             C               3            0         0
    01,01,2018 08:10             C               4            0         0
    01,01,2018 08:11             C               5            0         0
    01,01,2018 08:12             A               1            3         0
    01,01,2018 08:13             A               2            1         0
    01,01,2018 08:14             B               1            0         1
    01,01,2018 08:15             B               2            0         1
    01,01,2018 08:16             B               3            0         1
    01,01,2018 08:17             C               1            8         0

我试过了

df['error']=np.where((df['condition']==B) & (df['count']<=5) & (df['value'].sum==0), 1, 0)

或者if/for循环,但我会出错。你知道吗

间隔中的每个错误值都有1并不重要,或者只是间隔中的一行,只要1出现在某个地方就足够了,这样我就可以识别它。也许,间隔可以选择看前3分钟时,条件B开始,而不是计数(计数只是听起来更容易我)。你知道吗

你知道吗?提前感谢:)


Tags: 数据dfdate间隔valuecount情况error
1条回答
网友
1楼 · 发布于 2024-03-29 11:05:05

以下内容如何:

df['rolling'] = df['Value'].rolling(3).sum()

df['error'] = np.where((df['condition'] == 'B') & (df['count'].ge(3)),
                        df['rolling'].eq(0), np.nan)
df['error'] = np.where(df['condition'] == 'B',
                       df['error'].bfill(), 0)

这将为您提供:

             date condition  count  Value  rolling  error
01,01,2018  08:00         A      1      0      NaN    0.0
01,01,2018  08:01         A      2      1      NaN    0.0
01,01,2018  08:02         A      3      4      5.0    0.0
01,01,2018  08:03         B      1      2      7.0    0.0
01,01,2018  08:04         B      2      1      7.0    0.0
01,01,2018  08:05         B      3      7     10.0    0.0
01,01,2018  08:06         B      4      0      8.0    0.0
01,01,2018  08:07         C      1     11     18.0    0.0
01,01,2018  08:08         C      2      2     13.0    0.0
01,01,2018  08:09         C      3      0     13.0    0.0
01,01,2018  08:10         C      4      0      2.0    0.0
01,01,2018  08:11         C      5      0      0.0    0.0
01,01,2018  08:12         A      1      3      3.0    0.0
01,01,2018  08:13         A      2      1      4.0    0.0
01,01,2018  08:14         B      1      0      4.0    1.0
01,01,2018  08:15         B      2      0      1.0    1.0
01,01,2018  08:16         B      3      0      0.0    1.0
01,01,2018  08:17         C      1      8      8.0    0.0

如果您不需要'rolling'列,我们可以将其压缩为:

df['error'] = np.where((df['condition'] == 'B') & (df['count'].ge(3)),
                       df['Value'].rolling(3).sum().eq(0), np.nan)
df['error'] = np.where(df['condition'] == 'B',
                       df['error'].bfill(), 0)

相关问题 更多 >