如何使用pandas读取目录中所有文件的内容? [英] How can I read the contents of all the files in a directory with pandas?
本文介绍了如何使用pandas读取目录中所有文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件夹,里面有很多 .txt
文件.如何读取文件夹中的所有文件并使用 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_
推荐答案
是这样的:
import glob
l = [pd.read_csv(filename) for filename in glob.glob("/path/*.txt")]
df = pd.concat(l, axis=0)
您必须考虑标题,例如,如果您想忽略它,请查看 read_csv.
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.
这篇关于如何使用pandas读取目录中所有文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文