Import multiple csv files into pandas and concatenate into one DataFrame

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

    Almost all of the answers here are either unnecessarily complex (glob pattern matching) or rely on additional 3rd party libraries. You can do this in 2 lines using everything Pandas and python (all versions) already have built in.

    For a few files - 1 liner:

    df = pd.concat(map(pd.read_csv, ['data/d1.csv', 'data/d2.csv','data/d3.csv']))
    

    For many files:

    from os import listdir
    
    filepaths = [f for f in listdir("./data") if f.endswith('.csv')]
    df = pd.concat(map(pd.read_csv, filepaths))
    

    This pandas line which sets the df utilizes 3 things:

    1. Python's map (function, iterable) sends to the function (the pd.read_csv()) the iterable (our list) which is every csv element in filepaths).
    2. Panda's read_csv() function reads in each CSV file as normal.
    3. Panda's concat() brings all these under one df variable.

提交回复
热议问题