Jakiś problem?

How to combine multiple CSV files with 8 lines of code

Step 1: Import packages and set the working directory

Change “/mydir” to your desired working directory.

import os
import glob
import pandas as pd
os.chdir("/mydir")

Step 2: Use glob to match the pattern ‘csv’

Match the pattern (‘csv’) and save the list of file names in the ‘all_filenames’ variable. You can check out this link to learn more about regular expression matching.

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

Step 3: Combine all files in the list and export as CSV

Use pandas to concatenate all files in the list and export as CSV. The output file is named “combined_csv.csv” located in your working directory.

#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')

encoding = ‘utf-8-sig’ is added to overcome the issue when exporting ‘Non-English’ languages.

And…it’s done!

https://www.freecodecamp.org/news/how-to-combine-multiple-csv-files-with-8-lines-of-code-265183e0854/

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *