Import multiple csv files into pandas and concatenate into one DataFrame Import multiple csv files into pandas and concatenate into one DataFrame python 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 pdimport globpath = r'C:\DRO\DCL_rawdata_files' # use your pathall_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 darindaCoder's answer:

path = r'C:\DRO\DCL_rawdata_files'                     # use your pathall_files = glob.glob(os.path.join(path, "*.csv"))     # advisable to use os.path.join as this makes concatenation OS independentdf_from_each_file = (pd.read_csv(f) for f in all_files)concatenated_df   = pd.concat(df_from_each_file, ignore_index=True)# doesn't create a list, nor does it append to one


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


matomo