Merging two CSV files by a common column python

后端 未结 2 1333
春和景丽
春和景丽 2021-01-21 17:08

I am trying to merge two csv files with a common id column and write the merge to a new file. I have tried the following but it is giving me an error -

import c         


        
相关标签:
2条回答
  • 2021-01-21 17:16

    Here is an example using pandas

    import sys
    from StringIO import StringIO
    import pandas as pd
    
    TESTDATA=StringIO("""DOB;First;Last
        2016-07-26;John;smith
        2016-07-27;Mathew;George
        2016-07-28;Aryan;Singh
        2016-07-29;Ella;Gayau
        """)
    
    list1 = pd.read_csv(TESTDATA, sep=";")
    
    TESTDATA=StringIO("""Date of Birth;Patient First Name;Patient Last Name
        2016-07-26;John;smith
        2016-07-27;Mathew;XXX
        2016-07-28;Aryan;Singh
        2016-07-20;Ella;Gayau
        """)
    
    
    list2 = pd.read_csv(TESTDATA, sep=";")
    
    print list2
    print list1
    
    common = pd.merge(list1, list2, how='left', left_on=['Last', 'First', 'DOB'], right_on=['Patient Last Name', 'Patient First Name', 'Date of Birth']).dropna()
    print common
    
    0 讨论(0)
  • 2021-01-21 17:22

    Thanks Shijo.

    This is what worked for me after - merged by the first column in each csv.

    import csv
    from collections import OrderedDict
    
    with open('stops.csv', 'rb') as f:
        r = csv.reader(f)
        dict2 = {row[0]: row[1:] for row in r}
    
    with open('stops2.csv', 'rb') as f:
        r = csv.reader(f)
        dict1 = OrderedDict((row[0], row[1:]) for row in r)
    
    result = OrderedDict()
    for d in (dict1, dict2):
        for key, value in d.iteritems():
             result.setdefault(key, []).extend(value)
    
    with open('ab_combined.csv', 'wb') as f:
        w = csv.writer(f)
        for key, value in result.iteritems():
            w.writerow([key] + value)
    
    0 讨论(0)
提交回复
热议问题