【发布时间】:2025-12-28 10:35:06
【问题描述】:
这几天我一直在努力对列进行一些修改。batt_status 列的计算如下:
如果 (batt_input) > 0:
batt_status[i] = batt_status[i-1] + (batt_input[i] / 4)
其他:
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
我用过代码:
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)
正如您在batt_status 列中所指出的,可能会累积负值或强正值。现在我想限制batt_status列中的值,使它们达到最小值0和最大值20。
然后,当达到零时,后续值应该重复零,直到可以添加其他值(batt_input > 0)。
一旦达到值 20,它也应该被复制,直到可以减去一些东西 (batt_output > 0)。以此类推。
预期输出(示例):
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
你对此有什么想法吗?
【问题讨论】:
-
你能以reproducible的方式提供你的数据集吗?
-
@sophcles 刚刚添加,希望对您有所帮助
-
谢谢,不过还是不太理想。请问您也可以添加您想要的输出吗?
-
对不起,我忘记了最重要的事情在我的脑海中。我添加了预期的输出
标签: python pandas dataframe loops