将数字添加到公式中的变量,直到达到最大值 python

Add number to a variable in a formula until a maximum value is reached python

我有以下问题 -
我想设置一个 python 代码,它向公式中的变量添加一个浮点值(例如 0.1),直到达到最大值。 我从 Excel file 得到了不同的值,所以我尝试了以下代码:

import pandas as pd

df = pd.read_csv("dataset.csv", sep=";", decimal=",", encoding="latin1",dtype={"A": float, "G": float, "K": float, "V": float})

df["k"] = (df["A"] * df["V"] / 100 + df["G"] - df["K"]) / df["V"] * 100
while df["k"] < 15:
    df["k"] = (df["A"] * df["V"] / 100 + df["G"] - df["K"]) / df["V"] * 100
    df["A"] = df["A"] + 0.01

print([df])

当我尝试执行代码时出现以下错误:

Traceback (most recent call last):
  File "C:\xx\xx\x\pythonProject\Test x+1.py", line 10, in <module>
    while df["k"] < 15:
  File "C:\xx\xx\x\envs\pythonProject\lib\site-packages\pandas\core\generic.py", line 1442, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

你知道这个问题的原因和解决方法吗?

样本:

  1. V A G K
  2. 500 26,27 41,68 173,03 1000 26,27 41,68 304,38
  3. 1500 26,27 41,68 435,73 2000 26,27 41,68 567,08
  4. 2500 26,27 41,68 698,43 3000 26,27 41,68 829,78
  5. 3500 26,27 41,68 961,13 4000 26,27 41,68 1092,48

创建一个函数来求解 A:

输入数据:

# df["k"] = (df["A"] * df["V"] / 100 + df["G"] - df["K"]) / df["V"] 
>>> df
      V      A      G        K             k
0   500  26.27  41.68   173.03  0.000000e+00
1  1000  26.27  41.68   304.38  0.000000e+00
2  1500  26.27  41.68   435.73  0.000000e+00
3  2000  26.27  41.68   567.08 -5.684342e-15
4  2500  26.27  41.68   698.43  0.000000e+00
5  3000  26.27  41.68   829.78  0.000000e+00
6  3500  26.27  41.68   961.13  0.000000e+00
7  4000  26.27  41.68  1092.48  0.000000e+00
def solve_A(sr):
    A = sr["A"]
    k = sr["k"]
    while k < 15:
        prev_A = A
        prev_k = k
        A += 0.1
        k = (A * sr["V"] / 100 + sr["G"] - sr["K"]) / sr["V"] * 100
    sr["A"] = prev_A
    sr["k"] = prev_k
    return sr

out = df.apply(solve_A, axis="columns")

输出结果:

>>> out
        V      A      G        K     k
0   500.0  41.17  41.68   173.03  14.9
1  1000.0  41.17  41.68   304.38  14.9
2  1500.0  41.17  41.68   435.73  14.9
3  2000.0  41.17  41.68   567.08  14.9
4  2500.0  41.17  41.68   698.43  14.9
5  3000.0  41.17  41.68   829.78  14.9
6  3500.0  41.17  41.68   961.13  14.9
7  4000.0  41.17  41.68  1092.48  14.9

备选方案:不使用循环,直接求解A

样本:

# df["k"] = (df["A"] * df["V"] / 100 + df["G"] - df["K"]) / df["V"] * 100
>>> df
    A   G   K   V          k
0  27  14  18  14  -1.571429  # OK
1  24  18  16  26  31.692308  # <- change A
2  13  18  19  24   8.833333  # OK
3  23  23  18  27  41.518519  # <- change A
4  22  13  15  23  13.304348  # OK

用 A 表达你的等式:

k = 15 - 0.1
A = (-100*G + 100*K + V*k)/V

# Compute A
>>> (-100*18 + 100*16 + 26*14.99)/26
7.297692307692308

# Check k
>>> (7.297692307692308 * 24 / 100 + 18 - 19) / 24 * 100
14.990000000000004

所以:

mask = df.loc[df['k'] >= 15]

A = ((-100*df['G'] + 100*df['K'] + df['V']*14.99)/df['V'])

df.loc[mask, 'A'] = A.loc[mask]
df.loc[mask, 'k'] = 14.99
>>> df
           A   G   K   V          k
0  27.000000  14  18  14  -1.571429
1   7.297692  18  16  26  14.990000
2  13.000000  18  19  24   8.833333
3  -3.528519  23  18  27  14.990000
4  22.000000  13  15  23  13.304348

提示:使用SymPy

表达A
# pip install sympy
from sympy import symbols, solve, Eq

A, G, K, V, k = symbols('A G K V k')
expr = Eq((A * V / 100 + G - K) / V * 100, k)
>>> solve(expr, A)[0]
(-100*G + 100*K + V*k)/V