我有两个数据格式,在这两个数据中,我碰巧将'timeStamp‘设置为索引。df_1.set_index('timeStamp', inplace=True)。
df_1
value
timeStamp
2016-11-23 20:00:00 37.21
2016-11-23 21:00:00 37.79
2016-11-23 22:00:00 33.99
2016-11-23 23:00:00 32.66
2016-11-24 00:00:00 31.61df_2
value
timeStamp
2016-11-23 23:00:00 32.92
2016-11-24 00:00:00 31.54
2016-11-24 01:00:00 29.14当时间被共享时,我想做一个比较这两个值的数据。我试了combined_df= pd.merge(df_real, df_fc, on='timeStamp', how='inner'),得到了key error。
因此,我没有将两个数据文件合并在一个索引上,而是保留了不使用“timeStamp”作为索引的数据文件。例如。
我用df来合并
timeStamp value
0 2016-11-23 20:00:00 37.21
1 2016-11-23 21:00:00 37.79
2 2016-11-23 22:00:00 33.99
3 2016-11-23 23:00:00 32.66
13 2016-11-24 00:00:00 31.61然后我能够合并并设置了新的df (如下图所示)。随后,我还将索引设置为时间戳。
timeStamp value_x value_y
0 2016-11-23 23:00:00 32.66 32.92 我的问题为什么我不能合并被指定为索引的列名?我想把合并设置成一个新的数据.
发布于 2018-05-09 09:18:44
我相信你可以在索引上合并。你只是使用了错误的语法。与其指定on,不如尝试使用left_index和right_index。
发布于 2018-05-09 09:23:50
您需要指示您要在索引上合并:
pd.merge(df_1, df_2, left_index=True, right_index=True, how='inner')发布于 2018-05-09 09:43:07
DataFrame连接/合并
pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=True,
suffixes=('_x', '_y'), copy=True, indicator=False,
validate=None)https://stackoverflow.com/questions/50249653
复制相似问题