首页 > 解决方案 > Iterating data with cumulative sum over specific threshold

问题描述

Suppose that I have a column X and its index i as follows:

index X 0 0.007934 1 0.015535 2 0.000368 3 0.000212 4 0.000111 5 -0.001123 6 -0.000539 7 -0.000142 8 -0.000114 9 -0.002034 10 -0.041414 ... I want to iterate over it to create a new column Y that satisfies: - If the index i is the first exit index of that the cumulative sum of X_i exceed threshold 0.0006 then the value of Y at index i will be recorded as $Y_i = 'Yes' - Otherwise it is recorded as $Y_i = 'No'

Below is illustration for first 7 indexes:

abs(cumulative sum(X[:0]) = 0.007934 > 0.0006 then Y_0 = Yes.

abs(cumulative sum(X[1:1]) = 0.015535 > 0.0006 then Y_1 = Yes

abs(cumulative sum(X[2:2]) = 0.000368 < 0.0006 then Y_2 = No

abs(cumulative sum(X[2:3]) = 0.000580 < 0.0006 then Y_3 = No

abs(cumulative sum(X[2:4]) = 0.000691 > 0.0006 then Y_4 = Yes

abs(cumulative sum(X[5:5]) = 0.001123 > 0.0006 then Y_5 = Yes.

abs(cumulative sum(X[6:6]) = 0.000539 < 0.0006 then Y_6 = No

abs(cumulative sum(X[6:7]) = 0.000681 > 0.0006 then Y_7 = Yes

...

Iterate until the sample is depleted.

Can you help me solve this problem? Thank you very much.

标签: pythoniteration

解决方案


更新我已根据您的评论更正了代码。如果我正确理解它,那么解决您的问题的代码可能如下所示:

def Y(x, x_history):
    x_history.append(abs(x))
    if sum(x_history) > 0.0006:
        return True, []
    return False, x_history

X = [0.007934, 0.015535, 0.000368, 0.000212, 0.000111, -0.001123, -0.000539,
     -0.000142, -0.000114, -0.002034, -0.041414, 0.002792, 0.016099, 0.006480,
     -0.007141, -0.010191, -0.044790, -0.004887, -0.009217, -0.039200,
     -0.066433, 0.021649, 0.004331, -0.000393, -0.005410, 0.006222, 0.001342,
     0.065700, 0.003055, -0.004560]

print('index   X    Y')
x_history = []
for i, x in enumerate(X):
    y, x_history = Y(x, x_history)
    print(i, x, 'Yes' if y else 'No')

输出是:

index   X    Y
0 0.007934 Yes
1 0.015535 Yes
2 0.000368 No
3 0.000212 No
4 0.000111 Yes
5 -0.001123 Yes
6 -0.000539 No
7 -0.000142 Yes
8 -0.000114 No
9 -0.002034 Yes
10 -0.041414 Yes
11 0.002792 Yes
12 0.016099 Yes
13 0.00648 Yes
14 -0.007141 Yes
15 -0.010191 Yes
16 -0.04479 Yes
17 -0.004887 Yes
18 -0.009217 Yes
19 -0.0392 Yes
20 -0.066433 Yes
21 0.021649 Yes
22 0.004331 Yes
23 -0.000393 No
24 -0.00541 Yes
25 0.006222 Yes
26 0.001342 Yes
27 0.0657 Yes
28 0.003055 Yes
29 -0.00456 Yes

推荐阅读