Welcome folks today in this post we will be merging
or combining
multiple csv
files in python using pandas
library. All the source code of the application will be given below.
Get Started
In order to get started you need to install the following library using the pip
command as shown below
pip install pandas
After installing this library make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 |
import os import glob import pandas as pd os.chdir("/uploads") extension = 'csv' all_filenames = [i for i in glob.glob('*.{}'.format(extension))] #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') |
Before executing the python script you need to create the directory called as uploads
where you will put all the input csv
files which needs to be merged like this
Now you execute the python script by typing the below command as shown below
python app.py