重置索引,然后合并多个(列-)键:dfLeft.reset_index(inplace=True)
dfRight.reset_index(inplace=True)
dfMerged = pd.merge(dfLeft, dfRight,
left_on=['date', 'cusip'],
right_on=['date', 'idc__id'],
how='inner')
然后可以将“日期”重置为索引:dfMerged.set_index('date', inplace=True)
下面是一个示例:raw1 = '''
2012-01-03 XXXX 4.5
2012-01-03 YYYY 6.2
2012-01-04 XXXX 4.7
2012-01-04 YYYY 6.1
'''
raw2 = '''
2012-01-03 XYXX 45.
2012-01-03 YYYY 62.
2012-01-04 XXXX -47.
2012-01-05 YYYY 61.
'''
import pandas as pd
from StringIO import StringIO
df1 = pd.read_table(StringIO(raw1), header=None,
delim_whitespace=True, parse_dates=[0], skiprows=1)
df2 = pd.read_table(StringIO(raw2), header=None,
delim_whitespace=True, parse_dates=[0], skiprows=1)
df1.columns = ['date', 'cusip', 'factorL']
df2.columns = ['date', 'idc__id', 'factorL']
print pd.merge(df1, df2,
left_on=['date', 'cusip'],
right_on=['date', 'idc__id'],
how='inner')
它给予date cusip factorL_x idc__id factorL_y
0 2012-01-03 00:00:00 YYYY 6.2 YYYY 62
1 2012-01-04 00:00:00 XXXX 4.7 XXXX -47