Use pandas

How to append .csv files with Python pandas

How to append .csv files with Python pandas

Suppose you have a set of .csv files that you need to combine into one file, while keeping the header information.

Luckily, using the pandas package in Python, you can do this relatively easily.

In this example, you have two .csv files. homes.csv and homes1.csv

homes.csv

AddressPriceBedrooms
123 Main St99,0001
4981 Anytown Rd199,0004
132 Walrus ave299,0012
1506 Guido St784,0493

homes1.csv

AddressPriceBedrooms
491 Python St293,9234
938 Zeal Rd148,3982
247 Fort St299,2383
992 Settled St823,0494

How to append multiple .csv files with pandas

import pandas as pd
# Read in your .csv files as dataframes using pd.read_csv()
df_homes = pd.read_csv("C:/Users/kennethcassel/homes.csv")
df_homes1 = pd.read_csv("C:/Users/kennethcassel/homes1.csv")
# This method combines a list of pandas dataframes into one dataframe
pd.concat([df_homes, df_homes1])
# pd.concat accepts a list as an argument. If you had another homes.csv
# file to append you could add it to the list
# like pd.concat([df_homes, df_homes1, df_homes2])

If you'd like to export the file as a new csv, your code will look like this:

import pandas as pd
df_homes = pd.read_csv("C:/Users/kennethcassel/homes.csv")
df_homes1 = pd.read_csv("C:/Users/kennethcassel/homes1.csv")
pd.concat([df_homes, df_homes1]).to_csv('homes_complete.csv', index=False)

Resulting combined .csv file

homes_complete.csv

AddressPriceBedrooms
123 Main St99,0001
4981 Anytown Rd199,0004
132 Walrus ave299,0012
1506 Guido St784,0493
491 Python St293,9234
938 Zeal Rd148,3982
247 Fort St299,2383
992 Settled St823,0494
Edit this page on GitHub