python – Import multiple csv files into pandas and concatenate into one DataFrame

python – Import multiple csv files into pandas and concatenate into one DataFrame

If you have same columns in all your csv files then you can try the code below.
I have added header=0 so that after reading csv first row can be assigned as the column names.

import pandas as pd
import glob

path = rC:DRODCL_rawdata_files # use your path
all_files = glob.glob(path + /*.csv)

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

An alternative to darindaCoders answer:

path = rC:DRODCL_rawdata_files                     # use your path
all_files = glob.glob(os.path.join(path, *.csv))     # advisable to use os.path.join as this makes concatenation OS independent

df_from_each_file = (pd.read_csv(f) for f in all_files)
concatenated_df   = pd.concat(df_from_each_file, ignore_index=True)
# doesnt create a list, nor does it append to one

python – Import multiple csv files into pandas and concatenate into one DataFrame

import glob
import os
import pandas as pd   
df = pd.concat(map(pd.read_csv, glob.glob(os.path.join(, my_files*.csv))))

Leave a Reply

Your email address will not be published. Required fields are marked *