我想合并df1& df2所以我给df基于df1列的'高'价格。我认为最好的方法是使用df1?中的'date'列。我们的目标是将'df1 high'与'df2 datetime'对齐,这样我就可以看到当天的高点达到了什么时候。所以我应该用df1第0行和df2第51行返回一个df。 这就是我试过的 -
df21 = pd.merge(df1, df2, on='date', how='right')#no good
df22 = pd.merge(df1, df2, on='date', how='left')#returns nothing
df23 = pd.merge(df1, df2, on='date', how='inner')#returns nothing
df26 = pd.merge(df1, df2, right_index=True, left_index=True)#returns nothing
df29 = pd.merge(df1, df2, left_on='date', right_on='date')#returns nothing
df30 = pd.merge(df1, df2, right_index=True, left_index=True)#returns nothing
我有以下df1
date open high low close
0 2017-07-19 30.75 30.84 30.23 30.38
1 2017-07-20 30.44 30.97 29.90 30.52
2 2017-07-21 30.40 31.48 30.30 31.12
3 2017-07-22 31.89 31.98 31.11 31.49
4 2017-07-25 31.85 31.85 29.62 29.78
我有df2
datetime close high low open OI V WAP \
49 2017-07-19 10:19:00 30.62 30.70 30.60 30.69 91 196 30.654
50 2017-07-19 10:20:00 30.68 30.73 30.64 30.64 135 281 30.699
51 2017-07-19 10:21:00 30.71 30.84 30.69 30.69 383 1156 30.768
52 2017-07-19 10:22:00 30.73 30.74 30.69 30.70 40 88 30.710
53 2017-07-19 10:23:00 30.79 30.83 30.74 30.74 165 313 30.796
date time
49 2017-07-19 10:19:00
50 2017-07-19 10:20:00
51 2017-07-19 10:21:00
52 2017-07-19 10:22:00
53 2017-07-19 10:23:00
答案 0 :(得分:1)
您需要合并两个键left_on=['date', 'high']
,right_on=['date', 'high']
In [709]: df1.merge(df2, left_on=['date', 'high'], right_on=['date', 'high'])
Out[709]:
date open_x high low_x close_x datetime close_y \
0 2017-07-19 30.75 30.84 30.23 30.38 2017-07-19 10:21:00 30.71
low_y open_y OI V WAP time
0 30.69 30.69 383 1156 30.768 10:21:00
然后,根据需要删除或重命名列。