Import multiple csv files into pandas and concatenate into one DataFrame

前端 未结 16 1788
既然无缘
既然无缘 2020-11-21 07:47

I would like to read several csv files from a directory into pandas and concatenate them into one big DataFrame. I have not been able to figure it out though. Here is what I

16条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 07:59

    import pandas as pd
    import glob
    
    path = r'C:\DRO\DCL_rawdata_files' # use your path
    file_path_list = glob.glob(path + "/*.csv")
    
    file_iter = iter(file_path_list)
    
    list_df_csv = []
    list_df_csv.append(pd.read_csv(next(file_iter)))
    
    for file in file_iter:
        lsit_df_csv.append(pd.read_csv(file, header=0))
    df = pd.concat(lsit_df_csv, ignore_index=True)
    

提交回复
热议问题