我正在尝试合并两个Python数据帧,如下所示:
df1.head()
Out[36]:
Date Open High Low Close Adj Close Volume
0 2009-12-31 30.447144 30.478571 30.080000 30.104286 26.061205 88102700
1 2010-01-04 30.490000 30.642857 30.340000 30.572857 26.466835 123432400
2 2010-01-05 30.657143 30.798571 30.464285 30.625713 26.512596 150476200
3 2010-01-06 30.625713 30.747143 30.107143 30.138571 26.090879 138040000
4 2010-01-07 30.250000 30.285715 29.864286 30.082857 26.042646 119282800
df2.head()
Out[37]:
timestamp var1
0 2018-05-02 04:53:46 150785
1 2018-05-02 06:38:58 150785
2 2018-05-03 00:35:25 145510
3 2018-05-03 06:33:53 145510
4 2018-05-03 06:48:56 145510
使用以下命令:
merged1 = pd.merge(df1, df2, left_on='timestamp', right_on='Date')
给我一个错误ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat
我该如何解决这个问题呢?
发布于 2020-07-16 18:24:31
df2['Date']=df2['Timestamp'].str[:10]
,然后在日期合并。因为hh:mm:ss在df2中不为空,所以您不能只执行df1['Date'] = pd.to_datetime(df1['Date'])
https://stackoverflow.com/questions/62932377
复制相似问题