0

I have 5 csv files in a folder: 1.csv, 2.csv, 3.csv, 4.csv, 5.csv. All files having the same structure and column names.

I would like all of the files to be in a single pandas dataframe, df. Is there anyway to achieve this?

Don99
  • 209
  • 1
  • 2
  • 9
  • 2
    Does this answer your question? [Loading multiple csv files of a folder into one dataframe](https://stackoverflow.com/questions/52289386/loading-multiple-csv-files-of-a-folder-into-one-dataframe) or [Import multiple csv files into pandas and concatenate into one DataFrame](https://stackoverflow.com/q/20906474/15497888) – Henry Ecker Jul 02 '21 at 23:57

2 Answers2

1
df1 = pd.read_csv('1.csv')
df2 = pd.read_csv('2.csv')
df1.concat(df2)
# this will concat rows from df1 into df2
fthomson
  • 773
  • 3
  • 9
1

glob can be a useful package for this, along with the pandas.concat method.

This approach creates a list of the *.csv files in a directory, reads each one to a DataFrame which is then appended to a list, and then the list is concatenated together into one DataFrame.

import glob
import pandas as pd

dfs = []
for fin in glob.glob('*.csv'):
  dfs.append(pd.read_csv(fin))
df = pd.concat(dfs)