Import multiple csv files into pandas and concatenate into one DataFrame

前端 未结 16 1772
既然无缘
既然无缘 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条回答
  •  遇见更好的自我
    2020-11-21 08:10

    An alternative to darindaCoder's answer:

    path = r'C:\DRO\DCL_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)
    # doesn't create a list, nor does it append to one
    

提交回复
热议问题