python - 将多个文件中的多个CSV文件读取到pandas DataFrame中

标签 python csv pandas

我想浏览同一目录中不同文件夹中包含的不同 csv 文件。我的文件夹位于我的工作目录中。我的文件夹命名为:

folder1, folder2,folder3

它们每个都有名称相同的 csv csv1.csv、csv2.csv

我尝试了这段代码:

import os
import re
import pandas as pd
from pandas.core.frame import DataFrame

rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir, topdown=False):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)

        if "csv1.csv" == fname:
            var= pd.read_csv(fname)

我可以打印该文件夹中 csv 文件的名称,但出现错误: IOError:文件 csv1.csv 不存在
可能是什么问题?

最佳答案

正如您在注释中看到的,您必须加入 rootDirdirNamefname

import os
import re
import pandas as pd
from pandas.core.frame import DataFrame

rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir, topdown=False):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)
        filepath = os.path.join(rootDir, dirName, fname)
        if "csv1.csv" == fname:
            var = pd.read_csv(filepath)
            print var.head()

os.path.join(path, *paths) :

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

On Windows, the drive letter is not reset when an absolute path component (e.g., r'\foo') is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

关于python - 将多个文件中的多个CSV文件读取到pandas DataFrame中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363154/

相关文章:

Python:根据字符数拆分 CSV

csv - GnuPlot:每条 CSV 线画线

python - 在 django 模板中解析和排序 csv 文件

python - 标记组中的最后一个元素是否包含 Pandas 中的特定字符串

Python计算数据框列中值的频率

Python pandas 滚动意味着没有窗口 num 固定

python - 如何使用 Dash HTML 组件显示 html 文件

python - Tensorflow C++ 评估性能比 Python 差

python - 使用 Flask 应用程序在本地计算机上出现 404 错误

python - 同时翻转 Numpy 数组中的两个值