我试图在python中循环遍历df并将B2添加到B3,然后检查总和是否等于或大于950,如果是,则返回A3的值,如果不取B2和B3的乘积,现在还添加B4。如果满足先决条件,则返回 A4 的值。重复此过程,直到 x 大于或等于 950。唯一需要注意的是,如果 B2 已经等于 1000,则 python 脚本将需要计算 C 列。
使用下表,我尝试做的是查看 B 列或“关键”列,并将 4 加到 87(等于 91)评估是否大于或等于 950,因为它不是将 91 加到 361 来得到452 然后 768 然后 927 最后 987 此时查看 A 列以查看满足所有条件的要求,其中“要求”列为 11。唯一的额外警告是“关键”列中的第一个值是否相等到 1000 开始,因为这意味着“要求”列中的相应值等于 0,此时我希望脚本从“基本”列开始,并重复上述步骤。有人可以帮忙吗?我感谢任何支持和帮助。
type here
要求 | 关键 | 必备 | 改善 | 增强 |
---|---|---|---|---|
6 | 04 | 0 | 0 | 0 |
7 | 087 | 0 | 0 | 0 |
8 | 361 | 0 | 0 | 0 |
9 | 316 | 0 | 0 | 0 |
10 | 159 | 0 | 0 | 0 |
11 | 60 | 0 | 0 | 0 |
12 | 10 | 0 | 0 | 0 |
13 | 3 | 1 | 0 | 0 |
19 | 0 | 18 | 2 | 2 |
20 | 0 | 83 | 19 | 19 |
21 | 0 | 196 | 44 | 44 |
22 | 0 | 266 | 137 | 137 |
23 | 0 | 208 | 254 | 254 |
24 | 0 | 120 | 215 | 215 |
25 | 0 | 55 | 147 | 147 |
26 | 0 | 34 | 90 | 90 |
27 | 0 | 9 | 48 | 48 |
28 | 0 | 8 | 19 | 19 |
29 | 0 | 1 | 18 | 18 |
30 | 0 | 0 | 5 | 5 |
31 | 0 | 1 | 1 | 1 |
32 | 0 | 0 | 1 | 1 |
试试这个:
import pandas as pd
# Sample data
data = {
'critical': [4, 87, 361, 768, 927, 987],
'essential': [1000, 452, 768, 927, 987, 123],
'requirement': [0, 11, 22, 33, 44, 55]
}
df = pd.DataFrame(data)
x = 0 # Initialize x
index = 0 # Initialize index
while x < 950 and index < len(df):
if index == 0 and df['critical'][index] == 1000:
x = df['essential'][index] + df['critical'][index]
index = df['essential'][index]
else:
x = df['critical'][index] + df['critical'][index + 1]
index = df['requirement'][index]
result = df['requirement'][index]
print("Result:", result)