如何在pandas数据帧连接中将表A中的A列与表B中的B列复用?

时间:2017-03-07 22:11:14

标签: mysql python-2.7 pandas

环境

Python version 2.7.11
Pandas version 0.18.1

假设我们有以下输入表A& B:

A

ID,FROM_YEAR,TYPE,AREA1,AREA2
1,2015,A,100,200
1,2015,B,100,200

ID,FROM_YEAR,TYPE,COEFFICIENT
1,2015,A,100,1.5
1,2015,B,100,2.0

理想输出:

ID,FROM_YEAR,TYPE,AREA1,AREA2
1,2015,A,100,**300**
1,2015,B,100,**400**

其中200 * 1.5 = 300& 200 * 2.0 = 400

MySQL查询是:

SELECT 
a.ID,
a.FROM_YEAR,
a.TYPE,
a.AREA1,
a.AREA2*b.COEFFICIENT AS AREA2
FROM a
INNER JOIN b
ON 
a.ID=b.ID, 
a.FROM_YEAR=b.FROM_YEAR,
a.TYPE=b.TYPE

是否可以在Python pandas中执行此操作?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用eval()方法:

In [11]: pd.merge(A, B, on=['ID','FROM_YEAR','TYPE']) \
           .eval('AREA2 = AREA2 * COEFFICIENT', inplace=False)
Out[11]:
   ID  FROM_YEAR TYPE  AREA1_x  AREA2  AREA1_y  COEFFICIENT
0   1       2015    A      100  300.0      100          1.5
1   1       2015    B      100  400.0      100          2.0