For a few days I have been struggling with some modification of the column.. The batt_status
column is calculated as follows:
if (batt_input) > 0
:
batt_status[i] = batt_status[i-1] + (batt_input[i] / 4)
else:
batt_status[i] = batt_status[i-1] - (batt_output[i] / 4).
batt_input batt_output batt_status
0 0.000000 0.000000 0.000000
1 2.739314 0.000000 0.684829
2 5.000000 0.000000 1.934829
3 5.000000 0.000000 3.184829
4 4.190054 0.000000 4.232342
5 4.627677 0.000000 5.389261
6 4.237302 0.000000 6.448587
7 1.251996 0.000000 6.761586
8 4.147673 0.000000 7.798504
9 2.921009 0.000000 8.528756
10 4.877213 0.000000 9.748060
11 5.000000 0.000000 10.998060
12 5.000000 0.000000 12.248060
13 5.000000 0.000000 13.498060
14 0.000000 3.120185 12.718013
15 0.000000 3.094523 11.944382
16 0.000000 3.711843 11.016422
17 0.000000 4.338085 9.931900
18 0.000000 4.173286 8.888579
19 0.000000 4.312411 7.810476
20 0.000000 4.345891 6.724003
21 0.000000 4.512739 5.595818
22 0.000000 4.543866 4.459852
23 0.000000 4.450718 3.347172
24 0.000000 4.511852 2.219209
25 0.000000 4.765721 1.027779
26 0.000000 4.713985 -0.150717
27 0.000000 4.604684 -1.301888
I used code:
df['batt_status'] = np.where(df['batt_input'] == 0, 0, np.nan)
for i in range(1, len(df)):
if(df.loc[i, 'batt_input'] > 0):
df.loc[i, 'batt_status'] = df.loc[i-1, 'batt_status'] + (df.loc[i, 'batt_input']/4)
else:
df.loc[i, 'batt_status'] = df.loc[i-1, 'batt_status'] - (df.loc[i, 'batt_output']/4)
As you noted in the batt_status
column, there can be accumulating negative or strong positive values. Now I would like to limit the values in the batt_status
column so that they reach min value 0 and max value 20.
Then, when zero is reached, subsequent values should duplicate zero until something else can be added (batt_input > 0
).
Once the value 20 is reached, it should also be duplicated until something can be subtracted (batt_output > 0
). And so on.
Expected output (example):
batt_input batt_output batt_status
0 0.000000 0.000000 0.000000
1 2.739314 0.000000 0.684829
2 5.000000 0.000000 1.934829
3 5.000000 0.000000 3.184829
4 4.190054 0.000000 4.232342
5 4.627677 0.000000 5.389261
6 4.237302 0.000000 6.448587
7 1.251996 0.000000 6.761586
8 4.147673 0.000000 7.798504
9 2.921009 0.000000 8.528756
10 4.877213 0.000000 9.748060
11 5.000000 0.000000 10.998060
12 5.000000 0.000000 12.248060
13 5.000000 0.000000 13.498060
14 0.000000 3.120185 12.718013
15 0.000000 3.094523 11.944382
16 0.000000 3.711843 11.016422
17 0.000000 4.338085 9.931900
18 0.000000 4.173286 8.888579
19 0.000000 4.312411 7.810476
20 0.000000 4.345891 6.724003
21 0.000000 4.512739 5.595818
22 0.000000 4.543866 4.459852
23 0.000000 4.450718 3.347172
24 0.000000 4.511852 2.219209
25 0.000000 4.765721 1.027779
26 0.000000 4.713985 0.000000 # (here batt_status reaches lower than 0, so put the smallest possible i.e. 0)
27 0.000000 4.604684 0.000000 # repeat 0
28 0.000000 3.567943 0.000000 # repeat 0
29 0.000000 2.344556 0.000000 # repeat 0
30 2.739314 0.000000 0.684829 # can add (batt_input/4)
31 10.35678 0.000000 3.274024
32 65.03452 0.000000 19.53265
33 3.452341 0.000000 20.00000 # (here batt_status reaches value greater than 20, so put as much as possible, i.e. 20)
34 2.345566 0.000000 20.00000 # repeat 20
35 45.56677 0.000000 20.00000 # repeat 20
36 0.000000 25.45600 13.63600 # can substract (batt_output/4)
37 0.000000 2.445552 13.02462
Do you have any idea for that?