简体   繁体   English

如何逐行合并两个csv文件

[英]How to merge two csv files line by line

I'm using python 3.0 in windows. 我在Windows中使用python 3.0。

i have 2 csv files 我有2个CSV文件

file1.csv file1.csv

0, 10,12
0,11,12
1,15,12
2, 17,12

file2.csv file2.csv

0, 2, 1
1,22, 1
3, 11, 1

output.csv output.csv

0, 10,12
0,11,12
0, 2, 1
1,15,12
1,22, 1
2, 17,12
3, 11, 1

i tried the following code 我尝试了以下代码

a = pd.read_csv('file1.csv')
b = pd.read_csv('file2.csv')
c = pd.concat([a, b], join='outer')
c.to_csv("Output.csv", index = False)

But my output is 但是我的输出是

0, 10,12
0,11,12
1,15,12
2, 17,12
0, 2, 1
1,22, 1
3, 11, 1

Can you give me some pointers please. 请给我一些指导。 I'm new to python. 我是python的新手。

You can concatenate and sort them with pandas: 您可以使用大熊猫将它们串联并排序:

df = pd.concat([a, b]).astype('str')

df = df.sort_values(list(df.columns))

df.to_csv('Output.csv', index=False)

This does not create the output file but it demonstrates how heapq.merge could help: 这不会创建输出文件,但是它演示了heapq.merge如何heapq.merge帮助:

from heapq import merge
inputs = [file(f) for f in ['file1.csv', 'file2.csv']]
for line in merge(*inputs):
    print line,

With the sample data this produces 利用样本数据可以产生

0, 10,12
0, 2, 1
0,11,12
1,15,12
1,22, 1
2, 17,12
3, 11, 1

However this differs from the sample output in the ordering of the initial lines: 但是,这与示例输出的初始行顺序不同:

0, 10,12
0,11,12
0, 2, 1

but I'm not sure how to produce this ordering. 但我不确定如何产生此订单。 The sample output lines do not appear to be ordered by character or numeric columns (a numeric ordering of the fields would presumably put 0, 2, 1 first). 样品输出线不出现由字符或数字列进行排序(在字段的数字排序大概放0, 2, 1第一)。

EDIT: it appears the lines are ordered as if spaces were not present. 编辑:看来行是有序的,好像没有空格。 The following example: 下面的例子:

from heapq import merge
def reader(f):
    for line in file(f):
        yield line.replace(' ',''), line
inputs = [reader(f) for f in ['file1.csv', 'file2.csv']]
for pair in merge(*inputs):
    print pair[1],

generates this ordering: 生成此顺序:

0, 10,12
0,11,12
0, 2, 1
1,15,12
1,22, 1
2, 17,12
3, 11, 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM