nyan314sn
nyan314sn

Reputation: 1936

Calculate a string of formula which uses pandas dataframe columns

Suppose I have a pandas dataframe whose column is are a,b,c and index are dates.

df = pd.DataFrame(columns = ["a","b","c"],index=range(5),data=np.random.rand(5,3))

And, I have a string called formula = (2*a+b)/c, where a,b,c here refer to columns of pandas data frame. What is the most efficient way to go about this?

The solution should give the same answer as this

(2*df["a"]+df["b"])/df["c"]

The bonus question is what if the formula contains lagged value formula = (2*a[-1]+b)/c, where a[-1] would use the data from previous row of column a. Thanks.

Upvotes: 3

Views: 1813

Answers (1)

Dishin H Goyani
Dishin H Goyani

Reputation: 7713

Use DataFrame.eval to evaluate a string describing operations on DataFrame columns.

formula = "(2*a+b)/c"

df.eval(formula)
0    6.432992
1    1.175234
2    3.274955
3    2.050857
4    7.605282
dtype: float64

Upvotes: 3

Related Questions