gpt4 book ai didi

python - Pandas 中的 Excel 公式计算

转载 作者:太空宇宙 更新时间:2023-11-03 17:35:24 26 4
gpt4 key购买 nike

假设我有一个像这样的字典,其键是 Excel 中的单元格引用,值是公式或整数。

input_dict = {
"B25": "=B2*B4",
"C25": "=C2*C4",
"D25": "=D2*D4",
"E25": "=E2*E4",
"F25": "=F2*F4",
"G25": "=G2*G4",
"B22": 0,
"C22": "=SUM(B22:B28)",
"D22": "=SUM(C22:C28)",
"E22": "=SUM(D22:D28)",
"F22": "=SUM(E22:E28)",
"G22": "=SUM(F22:F28)",
"B28": "=B19*B20*B22",
"C28": "=C19*C20*C22",
"D28": "=D19*D20*D22",
"E28": "=E19*E20*E22",
"F28": "=F19*F20*F22",
"G28": "=G19*G20*G22",
"B2": 1000,
"C2": 900,
"D2": 880,
"E2": 860,
"F2": 840,
"G2": 800,
"B4": 0.95,
"C4": 0.90,
"D4": 0.80,
"E4": 0.80,
"F4": 0.70,
"G4": 0.60,
"B19": 0.001,
"C19": 0.001,
"D19": 0.001,
"E19": 0.001,
"F19": 0.001,
"G19": 0.002,
"B20": 4,
"C20": 3,
"D20": 4,
"E20": 4,
"F20": 3,
"G20": 4
}

如何对此类数据进行计算。

我的方法是将字典转换为 DataFrame 并对其执行计算,但我陷入困境。

df = pd.DataFrame(list(input_dict.items()))

df 的输出如下所示。

    0   1
0 G22 =SUM(F22:F28)
1 G4 0.6
2 F2 840
3 D2 880
4 C20 3
5 C4 0.9
6 B28 =B19*B20*B22
7 F25 =F2*F4
8 B25 =B2*B4
9 G25 =G2*G4
10 C28 =C19*C20*C22
11 G28 =G19*G20*G22
12 F22 =SUM(E22:E28)
13 C25 =C2*C4
14 B19 0.001
15 E4 0.8
16 D22 =SUM(C22:C28)
17 D4 0.8
18 G2 800
19 E28 =E19*E20*E22
20 D20 4
21 G20 4
22 E25 =E2*E4
23 F20 3
24 G19 0.002
25 E22 =SUM(D22:D28)
26 C2 900
27 D25 =D2*D4
28 E2 860
29 D28 =D19*D20*D22
30 C19 0.001
31 F28 =F19*F20*F22
32 B20 4
33 B2 1000
34 F4 0.7
35 E19 0.001
36 D19 0.001
37 B4 0.95
38 B22 0
39 F19 0.001
40 C22 =SUM(B22:B28)
41 E20 4

如何在Python中执行类似Excel的计算?

预期输出如下

{
"B25": "950",
"C25": "810",
"D25": "704",
"E25": "688",
"F25": "588",
"G25": "480",
"B22": 0,
"C22": 950,
"D22": 1757.15,
"E22": 2454.1214,
"F22": 3710.908,
"G22": 4161.220736,
"B28": 0,
"C28": -2.85,
"D28": -7.0286,
"E28": -9.8164856,
"F28": -9.396914743,
"G28": -29.687264,
"B2": 1000,
"C2": 900,
"D2": 880,
"E2": 860,
"F2": 840,
"G2": 800,
"B4": 0.95,
"C4": 0.90,
"D4": 0.80,
"E4": 0.80,
"F4": 0.70,
"G4": 0.60,
"B19": 0.001,
"C19": 0.001,
"D19": 0.001,
"E19": 0.001,
"F19": 0.001,
"G19": 0.002,
"B20": 4,
"C20": 3,
"D20": 4,
"E20": 4,
"F20": 3,
"G20": 4
}

最佳答案

我编写了一个库 xlcalculator,它可以读取 Excel 文件,将具有受支持函数的公式解释为 Python,然后可以对公式进行计算。

使用您所说的问题的示例;

input_dict = {
"B4": 0.95,
"B2": 1000,
"B19": 0.001,
"B20": 4,
# B21
"B22": 1,
"B23": 2,
"B24": 3,
"B25": "=B2*B4",
"B26": 5,
"B27": 6,
"B28": "=B19*B20*B22",
"C22": "=SUM(B22:B28)",
}

from xlcalculator import ModelCompiler
from xlcalculator import Model
from xlcalculator import Evaluator

compiler = ModelCompiler()
my_model = compiler.read_and_parse_dict(input_dict)
evaluator = Evaluator(my_model)

for formula in my_model.formulae:
print("Formula", formula, "evaluates to", evaluator.evaluate(formula))

# cells need a sheet and Sheet1 is default.
evaluator.set_cell_value("Sheet1!B22", 100)
print("Formula B28 now evaluates to", evaluator.evaluate("Sheet1!B28"))
print("Formula C22 now evaluates to", evaluator.evaluate("Sheet1!C22"))

产生此输出;

Formula Sheet1!B25 evaluates to 950.0
Formula Sheet1!B28 evaluates to 0.004
Formula Sheet1!C22 evaluates to 967.004
Formula B28 now evaluates to 0.4
Formula C22 now evaluates to 1066.4

关于python - Pandas 中的 Excel 公式计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31260686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com