[英]Cumulative sum after 64 in Python
The following is my data, 以下是我的数据,
count
0 239
1 47
2 47
3 47
4 47
5 47
6 47
7 47
8 47
9 47
10 47
11 48
12 48
13 48
14 48
15 48
16 48
17 48
18 48
19 48
20 48
21 48
22 48
23 48
24 48
25 48
26 48
27 48
28 49
29 50
I want to get the cumulative sum of the counter value once it reaches 64. For eg, if my data is as follows, 我想在计数器值达到64时获得其累计值。例如,如果我的数据如下,
In [125]: x
Out[125]:
array([ 57., 57., 57., 57., 57., 57., 58., 58., 58., 58., 61.,
61., 62., 62., 1., 1., 11., 16., 16., 16., 16., 16.,
16., 22., 22., 22., 27., 28.])
My output should be, 我的输出应该是
array([ 57., 57., 57., 57., 57., 57., 58., 58., 58., 58., 61.,
61., 62., 62., 65., 65., 75., 80., 80., 80., 80., 80.,
80., 86., 86., 86., 91., 92.])
For the above mentioned example, the code i tried was as follows, 对于上述示例,我尝试过的代码如下:
(np.ediff1d(x, to_begin=[0])<0).cumsum()*64 + x
This code seems to work well for the example. 对于该示例,此代码似乎运行良好。
But for my original data, I am getting the following, 但是对于我的原始数据,我得到以下信息:
Count
239
111
.
.
.
112
.
.
.
113
114
whereas my expected output is, 而我的预期输出是
239
286
.
.
.
287
.
.
.
288
289
Whenever the first value is around 200, It doesn't seem to work well. 每当第一个值在200左右时,它似乎就无法正常工作。 I am not able to understand the flaw here.Can anybody please tell me what mistake I am doing here and what needs to be changed? 我无法理解这里的缺陷。有人可以告诉我我在这里犯什么错误以及需要更改什么吗?
Thanks 谢谢
This should work for you, if I am understanding your question correctly. 如果我正确理解了您的问题,这对您应该有用。 I take the first column and look for the number 28 (as a test), at which I point I start calculating the cumulative sum. 我在第一列中查找数字28(作为测试),从这一点开始计算累计总和。 You will want to change this to 64, unless I have misunderstood. 除非我有误解,否则您将需要将此值更改为64。
This method works, but I am sure there are shorter methods to do the same! 此方法有效,但是我敢肯定有更短的方法可以做到这一点!
data = ''' count
0 239
1 47
2 47
3 47
4 47
5 47
6 47
7 47
8 47
9 47
10 47
11 48
12 48
13 48
14 48
15 48
16 48
17 48
18 48
19 48
20 48
21 48
22 48
23 48
24 48
25 48
26 48
27 48
28 49
29 50 '''
cumulative_sum = 0
for line in data.splitlines():
try:
tmp = map(int,line.strip().split())
if tmp[0]>=28:
cumulative_sum += tmp[1]
except ValueError:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.