I am trying to parse a string calculation which is a column within a dataframe, if the calculation is static I can use the eval function. However this doesnt appear to work when you give it a column name.
import pandas as pd
calcs = {'a': [1,1],
'b': [1,1],
'c': [1,1],
'calc': ['result=a*b','result=a+b']}
df = pd.DataFrame(calcs, columns = ['a', 'b','c','calc'])
print(df)
a b c calc
1 1 1 a*b
1 1 1 a+b
can you please tell me how it would be possible to evaluate the calculation in the 'calc' column for each row in the dataframe.
question from:
https://stackoverflow.com/questions/65641499/how-to-parse-a-string-calculation-in-a-pandas-dataframe-column 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…