How can I read the contents of all the files in a directory with pandas?

前端 未结 1 549
小蘑菇
小蘑菇 2021-02-06 07:13

I have a folder with lots of .txt files. How can I read all the files in the folder and get the content of them with pandas?. I tried the following:



        
相关标签:
1条回答
  • 2021-02-06 07:50

    Something like this:

    import glob
    
    l = [pd.read_csv(filename) for filename in glob.glob("/path/*.txt")]
    df = pd.concat(l, axis=0)
    

    You have to take into account the header, for example if you want to ignore it take a look at the skiprows option in read_csv.

    0 讨论(0)
提交回复
热议问题