在 Python 中对多个数据帧的操作

Tom*_*ski 3 python dataframe pandas

提供数据框:

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

并列出 d = [A, C, B, B]

如何(((A + C) * B) - B)对帧值编写数学运算以创建新的数据帧?

例如,结果是以下形式的框架:

e = pd.DataFrame({'E':[8, 18]})
Run Code Online (Sandbox Code Playgroud)

Max*_*axU 5

IUC:

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
Run Code Online (Sandbox Code Playgroud)