简体   繁体   中英

combining several csv files into one with python not aligned

I have several csv files that I want to merge to one file.

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

output: 在此处输入图像描述

在此处输入图像描述

It seems you need axis=1 parameter in concat :

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

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