pandas 如何使用pandas读取目录中所有文件的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27749553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:48:30  来源:igfitidea点击:

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

pythonpython-2.7pandas

提问by newWithPython

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

我有一个文件夹,里面有很多.txt文件。如何读取文件夹中的所有文件并使用 Pandas 获取它们的内容?我尝试了以下方法:

import pandas as pd
list_=pd.read_csv("/path/of/the/directory/*.txt",header=None)
print list_

回答by elyase

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.

你如果要忽略它看一看在skiprows选项,考虑到头部,例如read_csv