为了使答案更笼统...首先我将采用公共索引来同步两个数据帧,然后将它们中的每一个加入我的模式(dates),并将列相加同名,最后加入两个数据框(删除其中一个添加的列),
你可以在这里看到一个例子(谷歌的股票价格取自谷歌):
import numpy as np
import pandas as pd
import datetime as dt
prices = pd.DataFrame([[553.0, 555.5, 549.3, 554.11, 0],
[556.8, 556.8, 544.05, 545.92, 545.92],
[545.5, 546.89, 540.97, 542.04, 542.04]],
index=[dt.datetime(2014,11,04), dt.datetime(2014,11,05), dt.datetime(2014,11,06)],
columns=['Open', 'High', 'Low', 'Close', 'Adj Close'])
corrections = pd.DataFrame([[0, 555.22], [1238900, 0]],
index=[dt.datetime(2014,11,3), dt.datetime(2014,11,4)],
columns=['Volume', 'Adj Close'])
dates = pd.DataFrame(prices.index, columns = ['Dates']).append(pd.DataFrame(corrections.index, columns = ['Dates'])).drop_duplicates('Dates').set_index('Dates').sort(axis=0)
df_corrections = dates.join(corrections).fillna(0)
df_prices = dates.join(prices).fillna(0)
for col in prices.columns:
if col in corrections.columns:
df_prices[col]+=df_corrections[col]
del df_corrections[col]
df_prices = df_prices.join(df_corrections)