将多个csv文件合并为一个未对齐的python

时间:2019-11-06 12:19:30

标签: python pandas csv

我有几个要合并到一个文件的csv文件。

import os
import glob
import pandas as pd

os.chdir('/home/yovel/PycharmProjects/fantasyfinal/stats')

extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]

combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames], sort=False)
combined_csv.to_csv("statsmerger.csv", index=True, encoding='utf-8-sig')

输出: enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

似乎您需要在concat中使用axis=1参数:

combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames], sort=False, axis=1)