30

好的,我已经在 Stack Overflow 上阅读了几个主题。我认为这对我来说相当容易,但我发现我仍然没有很好地掌握 Python。我尝试了位于How to combine 2 csv files with common column value 中的示例,但两个文件的行数不同,这很有帮助,但我仍然没有希望达到的结果。

本质上,我有 2 个带有共同第一列的 csv 文件。我想合并2。即

文件a.csv

标题,阶段,一月,二月
该死,3.001,0.421,0.532
好的,2.829,1.036,0.751
三、1.115、1.146、2.921

文件b.csv

标题,三月,四月,五月,六月,
该死,0.631,1.321,0.951,1.751
好的,1.001,0.247,2.456,0.3216
三,0.285,1.283,0.924,956

output.csv(不是我得到的,而是我想要的)

标题,阶段,1 月,2 月,3 月,4 月,5 月,6 月
该死,3.001,0.421,0.532,0.631,1.321,0.951,1.751
好的,2.829,1.036,0.751,1.001,0.247,2.456,0.3216
三、1.115、1.146、2.921、0.285、1.283、0.924、956

output.csv(我实际得到的输出)

标题,二月,五月
好的,0.751,2.456
三、2.921、0.924
该死,0.532,0.951

我正在尝试的代码:

'''
testing merging of 2 csv files
'''
import csv
import array
import os

with open('Z:\\Desktop\\test\\filea.csv') as f:
    r = csv.reader(f, delimiter=',')
    dict1 = {row[0]: row[3] for row in r}

with open('Z:\\Desktop\\test\\fileb.csv') as f:
    r = csv.reader(f, delimiter=',')
    #dict2 = {row[0]: row[3] for row in r}
    dict2 = {row[0:3] for row in r}

print str(dict1)
print str(dict2)

keys = set(dict1.keys() + dict2.keys())
with open('Z:\\Desktop\\test\\output.csv', 'wb') as f:
    w = csv.writer(f, delimiter=',')
    w.writerows([[key, dict1.get(key, "''"), dict2.get(key, "''")] for key in keys])

任何帮助是极大的赞赏。

4

2 回答 2

91

当我处理csv文件时,我经常使用pandas库。它使这样的事情变得非常容易。例如:

import pandas as pd

a = pd.read_csv("filea.csv")
b = pd.read_csv("fileb.csv")
b = b.dropna(axis=1)
merged = a.merge(b, on='title')
merged.to_csv("output.csv", index=False)

一些解释如下。首先,我们读入 csv 文件:

>>> a = pd.read_csv("filea.csv")
>>> b = pd.read_csv("fileb.csv")
>>> a
   title  stage    jan    feb
0   darn  3.001  0.421  0.532
1     ok  2.829  1.036  0.751
2  three  1.115  1.146  2.921
>>> b
   title    mar    apr    may       jun  Unnamed: 5
0   darn  0.631  1.321  0.951    1.7510         NaN
1     ok  1.001  0.247  2.456    0.3216         NaN
2  three  0.285  1.283  0.924  956.0000         NaN

我们看到有一个额外的数据列(注意第一行fileb.csv-- title,mar,apr,may,jun,-- 末尾有一个额外的逗号)。我们可以很容易地摆脱它:

>>> b = b.dropna(axis=1)
>>> b
   title    mar    apr    may       jun
0   darn  0.631  1.321  0.951    1.7510
1     ok  1.001  0.247  2.456    0.3216
2  three  0.285  1.283  0.924  956.0000

现在我们可以在标题列上合并a和:b

>>> merged = a.merge(b, on='title')
>>> merged
   title  stage    jan    feb    mar    apr    may       jun
0   darn  3.001  0.421  0.532  0.631  1.321  0.951    1.7510
1     ok  2.829  1.036  0.751  1.001  0.247  2.456    0.3216
2  three  1.115  1.146  2.921  0.285  1.283  0.924  956.0000

最后写出来:

>>> merged.to_csv("output.csv", index=False)

生产:

title,stage,jan,feb,mar,apr,may,jun
darn,3.001,0.421,0.532,0.631,1.321,0.951,1.751
ok,2.829,1.036,0.751,1.001,0.247,2.456,0.3216
three,1.115,1.146,2.921,0.285,1.283,0.924,956.0
于 2013-04-28T18:20:06.657 回答
1

您需要将所有额外的行存储在字典中的文件中,而不仅仅是其中一个:

dict1 = {row[0]: row[1:] for row in r}
...
dict2 = {row[0]: row[1:] for row in r}

然后,由于字典中的值是列表,您只需将列表连接在一起:

w.writerows([[key] + dict1.get(key, []) + dict2.get(key, []) for key in keys])
于 2013-04-28T18:07:22.840 回答