假设我有一个像这样的数据框
E25
我需要评估公式并将其放回同一行,列。 E.g "=E2*E4"
的值为E2
,我需要评估E4
和E25
并将其重新投入import re
d = {
"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,
"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",
}
a = list()
def split_inp(inp):
return re.match(r"([a-z]+)([0-9]+)", inp, re.I)
for k, v in d.items():
match = split_inp(k)
a.append((match.groups(), v))
import pandas as pd
df = pd.DataFrame()
formula_ = []
for i, j in a:
if isinstance(j, str) and j.startswith('=') and not j[1:].startswith('S'):
formula_.append((i, j))
df.set_value(i[0], i[1], j)
print(df)
operators = ['+', '-', '/', '*']
print(formula_)
def split_op(inp):
for i in operators:
if i in inp:
return inp.partition(i)
for i, j in formula_:
j = j[1:]
match = split_op(j)
print(match)
g = split_inp(match[0])
f = split_inp((match[2]))
new = (g.groups(), match[1], f.groups())
# print(new)
# df.set_value(i[0], [1], df.query(new[0][0], new[]))
# if isinstance(j, str) and j.startswith('=') and not j[1:].startswith('S'):
# j = j[1:]
# print(j)
# df.set_value(i[0], i[1], pd.eval(j))
# else:
# pass
。
我怎样才能实现这个目标?
这是到目前为止的代码
A1