在python 3中合并多个csv文件

时间:2017-10-12 01:08:26

标签: python csv merge python-3.6

我是python的新手并且正在分析涉及合并csv文件的大型数据集,它们都包含相同的标记行但具有不同的列数。我没有太多,但这是我目前的代码,非常感谢任何帮助。

user

2 个答案:

答案 0 :(得分:0)

来自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')

答案 1 :(得分:0)

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)
相关问题