2 回答
TA贡献1842条经验 获得超12个赞
# this will result in merged df wtih all intersecting dates
df1 = df1.merge(df2, on = 'DATE', suffixes=('','_df2')
# the column you need
df1['sub'] = df1['Value1'] - df1['Value1_df2']
# next drop unnecessary columns of df2
df1.drop(columns = ['Value1_df2', 'Value2_df2'], inplace=True)
TA贡献1836条经验 获得超4个赞
首先将索引设置为'DATE'使其对齐。然后我们减去。由于您似乎希望将输出添加到df2我们将执行-(df2 - df1)的操作与(df1 - df2)
df1 = df1.set_index('DATE')
df2 = df2.set_index('DATE')
df2['sub'] = -df2['Value1'].sub(df1['Value1'])
Value1 Value2 sub
DATE
04.01.05 2.7010 2.6995 0.0530
05.01.05 2.7065 2.7050 0.0251
07.01.05 2.6348 2.6333 0.1117
10.01.05 2.6350 2.6315 0.1035
11.01.05 2.6275 2.6265 0.1073
12.01.05 2.6268 2.6253 0.1080
13.01.05 2.6285 2.6270 0.1063
17.01.05 2.6565 2.6555 0.0800
18.01.05 2.6275 2.6260 0.1090
19.01.05 2.6430 2.6415 0.0935
添加回答
举报