用python减去两列不同的Dataframe

时间:2015-06-25 12:17:33

标签: python-2.7 pandas dataframe

我有两个DataFrame,df1:

     Lat1         Lon1     tp1
0   34.475000  349.835000   1
1   34.476920  349.862065   0.5
2   34.478833  349.889131   0
3   34.480739  349.916199   3
4   34.482639  349.943268   0
5   34.484532  349.970338   0

和df2:

      Lat2         Lon2    tp2
0   34.475000  349.835000   2
1   34.476920  349.862065   1
2   34.478833  349.889131   0
3   34.480739  349.916199   6
4   34.482639  349.943268   0
5   34.484532  349.970338   0

我想减去(tp1-tp2)列并创建一个新的数据帧,其列数为Lat1,lon1,tp1-tp2。有谁知道我该怎么办?

1 个答案:

答案 0 :(得分:5)

import pandas as pd

df3 = df1[['Lat1', 'Lon1']]
df3['tp1-tp2'] = df1.tp1 - df2.tp2


Out[97]: 
      Lat1      Lon1  tp1-tp2
0  34.4750  349.8350     -1.0
1  34.4769  349.8621     -0.5
2  34.4788  349.8891      0.0
3  34.4807  349.9162     -3.0
4  34.4826  349.9433      0.0
5  34.4845  349.9703      0.0