pandas 用python减去两列不同的Dataframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31050160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:31:45  来源:igfitidea点击:

subtract two columns of different Dataframe with python

python-2.7pandasdataframe

提问by Vanessa

I have two DataFrames, df1:

我有两个数据帧,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

and df2:

和 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

I want to substract (tp1-tp2) columns and create a new dataframe whose colums are Lat1,lon1,tp1-tp2. anyone know how can I do it?

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

回答by Jianxun Li

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