python pandas条件累积和 [英] python pandas conditional cumulative sum

查看:340
本文介绍了python pandas条件累积和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我的数据框 df

data  data_binary  sum_data
  2       1            1
  5       0            0
  1       1            1
  4       1            2
  3       1            3
  10      0            0
  7       0            0
  3       1            1

我想计算 data_binary的累积总和在连续的 1 值组中。

I want to calculate the cumulative sum of data_binary within groups of contiguous 1 values.

第一组 1 有一个 1 sum_data 只有一个 1 。但是,第二组 1 的有3 1 sum_data [1,2,3]

The first group of 1's had a single 1 and sum_data has only a 1. However, the second group of 1's has 3 1's and sum_data is [1, 2, 3].

我尝试过使用 np.where(df ['data_binary'] == 1,df ['data_binary']。cumsum(),0)但返回

array([1, 0, 2, 3, 4, 0, 0, 5])

这不是我想要的。

推荐答案

你想拿走累计金额 data_binary 并减去最近的累积金额,其中 data_binary 为零。

you want to take the cumulative sum of data_binary and subtract the most recent cumulative sum where data_binary was zero.

b = df.data_binary
c = b.cumsum()
c.sub(c.mask(b != 0).ffill(), fill_value=0).astype(int)

0    1
1    0
2    1
3    2
4    3
5    0
6    0
7    1
Name: data_binary, dtype: int64






解释


Explanation

让我们先来看看每一步都是sid并排

Let's start by looking at each step side by side

cols = ['data_binary', 'cumulative_sum', 'nan_non_zero', 'forward_fill', 'final_result']
print(pd.concat([
        b, c,
        c.mask(b != 0),
        c.mask(b != 0).ffill(),
        c.sub(c.mask(b != 0).ffill(), fill_value=0).astype(int)
    ], axis=1, keys=cols))


   data_binary  cumulative_sum  nan_non_zero  forward_fill  final_result
0            1               1           NaN           NaN             1
1            0               1           1.0           1.0             0
2            1               2           NaN           1.0             1
3            1               3           NaN           1.0             2
4            1               4           NaN           1.0             3
5            0               4           4.0           4.0             0
6            0               4           4.0           4.0             0
7            1               5           NaN           4.0             1

cumulative_sum 是 data_binary 为零的行,不要重置总和。这就是这个解决方案的动力。当 data_binary 为零时,我们如何重置总和?简单!我将 data_binary 为零的累积和切片并转发填充值。当我得到这个和累积金额之间的差异时,我已经有效地重置了总和。

The problem with cumulative_sum is that the rows where data_binary is zero, do not reset the sum. And that is the motivation for this solution. How do we "reset" the sum when data_binary is zero? Easy! I slice the cumulative sum where data_binary is zero and forward fill the values. When I take the difference between this and the cumulative sum, I've effectively reset the sum.

这篇关于python pandas条件累积和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆