Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

import pandas as pd
list_=pd.read_csv("/path/of/the/directory/*.txt",header=None)
print list_
like image 807
newWithPython Avatar asked Jan 02 '15 22:01

newWithPython


1 Answers

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.

like image 157
elyase Avatar answered Oct 10 '22 11:10

elyase