简体   繁体   中英

Merge multiple csv files in python 3

I'm new to python and working on analyzing large data sets which involved merging csv files, they all contain the same labeled rows but with different amounts of columns. I don't have much but this is my current code, any help is greatly appreciated.

import csv
import pandas as pd
file1 = 'C:\\Users\\User\\Documents\\Ubiome csv Kit #\\107-078-414.csv'
file2 = 'C:\\Users\\User\\Documents\\Ubiome csv Kit #\\109-080-426.csv'

reader1 = csv.reader(open(file1))
reader2 = csv.reader(open(file2))

reader1 = csv.reader(open(file1))
reader2 = csv.reader(open(file2))

From the pandas docs: https://pandas.pydata.org/pandas-docs/stable/merging.html

import pandas as pd
df1 = pd.read_csv(file1)
df2 = pd.read_csv(file2)
merged_df = pd.concat([df1, df2], axis = 1, join = 'outer')
import pymysql.cursors
import re
import csv
import collections
import glob

# Variables

total_record = []
headerCount = 0

for file in glob.glob("*.csv"):
    print(file)

    with open(file, 'r') as f:
        reader = csv.reader(f)
        list_record = list(reader)
        if headerCount == 0:
            headerCount = 1
            total_record.extend(list_record)
        else:
            list_record.pop(0)
            total_record.extend(list_record)

with open('combine.csv', 'w') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerows(total_record)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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