Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将两个csv文件合并到多个列和最近的日期时间上_Python_Csv_Pandas_Merge - Fatal编程技术网

Python将两个csv文件合并到多个列和最近的日期时间上

Python将两个csv文件合并到多个列和最近的日期时间上,python,csv,pandas,merge,Python,Csv,Pandas,Merge,我有两个csv文件,我想合并 文件1: rel_id, acc_id, value, timestamp 1, 2, True, 2016-01-04 19:20:22 2, 3, True, 2016-01-04 18:35:56 1, 2, True, 2016-01-04 20:43:12 1, 5, False, 2016-01-04 18:15:20 2, 3, True, 2016-01-04 20:43:11 文件2: rel_id, acc_id, value, timesta

我有两个csv文件,我想合并

文件1:

rel_id, acc_id, value, timestamp
1, 2, True, 2016-01-04 19:20:22
2, 3, True, 2016-01-04 18:35:56
1, 2, True, 2016-01-04 20:43:12
1, 5, False, 2016-01-04 18:15:20
2, 3, True, 2016-01-04 20:43:11
文件2:

rel_id, acc_id, value, timestamp
1, 2, 250, 2016-01-04 20:43:13
1, 5, 610, 2016-01-04 18:15:23
2, 3, 400, 2016-01-04 18:35:58
2, 3, 300, 2016-01-04 20:43:13
1, 2, 500, 2016-01-04 19:20:23
我想根据rel_id、acc_id和timestamp合并这两个文件

合并(文件1和文件2):

但是,file2的时间戳稍晚

通过搜索stackoverflow,我找到了以下帖子:

但我不知道如何处理rel_id、acc_id和最近时间戳的匹配

import pandas as pd


file1 = pd.read_csv('file1.csv')
file2 = pd.read_csv('file2.csv')


file1.columns = ['rel_id', 'acc_id', 'value', 'timestamp']
file2.columns = ['rel_id', 'acc_id', 'value', 'timestamp']


file1['timestamp'] = pd.to_datetime(file1['timestamp'])
file2['timestamp'] = pd.to_datetime(file2['timestamp'])


file1_dt = pd.Series(file1["timestamp"].values, file1["timestamp"])
file1_dt.reindex(file2["timestamp"], method="nearest")
file2["nearest"] = file1_dt.reindex(file2["timestamp"],    method="nearest").values

print file2
我根据另一篇文章尝试了上面的代码,但是rel_id和acc_id还不匹配。另外,上述代码已经引发了一个错误:

ValueError:索引必须是单调递增或递减的


任何帮助都是非常感谢的。谢谢。

您正在尝试根据未排序的索引重新编制索引。 假设您的CSV没有标题:

column_names = ['rel_id', 'acc_id', 'value', 'timestamp']
file1 = pd.read_csv('file1.csv',
                    index_col=['timestamp'],
                    parse_dates='timestamp',
                    header=None,
                    names=column_names).sort_index()
file2 = pd.read_csv('file2.csv',
                    index_col=['timestamp'],
                    parse_dates='timestamp',
                    header=None,
                    names=column_names).sort_index()
file1.set_index(file1.reindex(file2.index, method='nearest').index, inplace=True)



                     rel_id  acc_id  value
timestamp
2016-01-04 18:15:23       1       5  False
2016-01-04 18:35:58       2       3   True
2016-01-04 19:20:23       1       2   True
2016-01-04 20:43:13       2       3   True
2016-01-04 20:43:13       1       2   True
并合并文件1和文件2:

rel_id, acc_id, value_file1, timestamp, value_file2
1, 2, True, 2016-01-04 19:20:22, 500
2, 3, True, 2016-01-04 18:35:56, 400
1, 2, True, 2016-01-04 20:43:12, 250
1, 5, False, 2016-01-04 18:15:20, 610
2, 3, True, 2016-01-04 20:43:11, 300
file1.reset_index().merge(file2.reset_index(), on=['acc_id', 'rel_id', 'timestamp']).set_index('timestamp')

这不总是选择提前的文件吗?没有什么意义,或者我误解了什么?谢谢你的回答和例子。我觉得自己很愚蠢,但如何获得合并的输出(因此在file1或file2的时间戳上同时显示值列和合并)?当使用
pd.merged(file1,file2,on='timestamp')
时,我会得到一个错误。这可能是一种更方便的方法,但类似于:
file1.reset_index().merge(file2.reset_index(),on=['acc_id','rel_id','timestamp'])。set_index('timestamp')
。特别是现在您将
时间戳设置为索引,然后重置并再次设置它…Valtuart:谢谢您的帮助。我似乎还不能让它以正确的方式工作。我通过在file2数据中添加所需的输出和更改来编辑我的问题,以表明它并不总是按时间顺序排列的。在评论中使用建议的代码时,文件2的时间戳在2016-01-04 20:43:13处出错。对于rel_id=1和acc_id=2,它会显示此时间戳2次。谢谢valtuarte。它确实适用于样本数据,因此我检查了您的答案。然而不幸的是,我不能让它与我的实际数据一起工作。