Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operations on multiple Dataframes in Python

Data frames are provided:

a = pd.DataFrame({'A':[1, 2]})
b = pd.DataFrame({'B':[2, 3]})
C = pd.DataFrame({'C':[4, 5]})

and list d = [A, C, B, B]

How to write an mathematical operations (((A + C) * B) - B) on frame values to create a new data frame?

The result is, for example, a frame in the form:

e = pd.DataFrame({'E':[8, 18]})
like image 470
Tomasz Przemski Avatar asked Feb 04 '26 05:02

Tomasz Przemski


1 Answers

IIUC:

In [132]: formula = "E = (((A + C) * B) - B)"

In [133]: pd.concat([a,b,C], axis=1).eval(formula, inplace=False)
Out[133]:
   A  B  C   E
0  1  2  4   8
1  2  3  5  18

In [134]: pd.concat([a,b,C], axis=1).eval(formula, inplace=False)[['E']]
Out[134]:
    E
0   8
1  18
like image 172
MaxU - stop WAR against UA Avatar answered Feb 05 '26 20:02

MaxU - stop WAR against UA