-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeCSV.py
29 lines (21 loc) · 1.07 KB
/
mergeCSV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Step 1: Import packages and set the working directory
# Change “/csv” to your desired working directory.
import os
import glob
import pandas as pd
os.chdir("./csvs")
# 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))]
print("\n----> Collected csv files to merge = {}\n".format(len(all_filenames)))
# 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 “merged_csv.csv” located in your working directory.
# combine all files in the list
merged_csv = pd.concat([pd.read_csv(f) for f in all_filenames])
# export to csv
print("\n[ * ] - Merging cvs files as merged_data.csv\n")
merged_csv.to_csv("merged_data.csv", index=False, encoding='utf-8-sig')
print("\n[ * ] - Merged {} csv files !!!\n".format(len(all_filenames)))