3

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]})
4
  • 1
    You could also just do (a.A + C.C) * b.B - b.B quite simply. Commented Nov 19, 2017 at 22:26
  • @cᴏʟᴅsᴘᴇᴇᴅ Massacre:D Thanks :) Commented Nov 19, 2017 at 22:28
  • No problem. I still like Max's solution better, because it offers you much more flexibility. Commented Nov 19, 2017 at 22:32
  • @cᴏʟᴅsᴘᴇᴇᴅ And I'm still trying with the previous problem. I think he can do just that. Theoretically, I have only one function, let's assume that the solution to my problem would be to create a loop with the function ((A * B) * C) * A) and so on. Because in that solution the code gives separate solutions for each element, comparing them still to the first one :) Commented Nov 19, 2017 at 22:40

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.