Use pandas
How to Merge All .csv Files In a Folder On Windows
How to Merge All .csv Files In a Folder On Windows
Imagine you have a lot of .csv files to merge in a Windows folder. It'd take a decent amount of time to do this manually. With Python pandas, you can make quick work of this task and merge all .csv files using the following recipe.
Special thanks to fmaume for submitting this recipe.
Merge all .csv files in a folder
# glob is a library to find pathnames using regex patternsimport globimport pandas as pd# Folder containing the .csv files to mergefile_path = "G:\\Dropbox\\to merge"# This pattern \\* selects all files in a directorypattern = file_path + "\\*"files = glob.glob(pattern)# Import first file to initiate the dataframedf = pd.read_csv(files[0],encoding = "utf-8", delimiter = ",")# Append all the files as dataframes to the first onefor file in files[1:len(file_list)]: df_csv = pd.read_csv(file,encoding = "utf-8", delimiter = ",") df = df.append(df_csv)
🐼 Get pandas recipes straight to your inbox!
Join other Data Scientists/Analysts/Engineers in learning pandas deeper. No spam!