Import multiple csv files into pandas and concatenate into one DataFrame

前端 未结 16 1740
既然无缘
既然无缘 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:14

    This is how you can do using Colab on Google Drive

    import pandas as pd
    import glob
    
    path = r'/content/drive/My Drive/data/actual/comments_only' # 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,sort=True)
    frame.to_csv('/content/drive/onefile.csv')
    

提交回复
热议问题