简体   繁体   English

在给定某些约束的情况下,如何使用 Python 遍历目录中的文件和 output 和 pandas 数据框?

[英]How can I use Python to walk through files in directories and output a pandas data frame given certain constraints?

So I'm using Pyhton, and I have a parent directory, with two child directories, in turn containing many directories, each with three files.所以我使用 Pyhton,我有一个父目录,有两个子目录,依次包含许多目录,每个目录有三个文件。 I want to take the third file (which is a.CSV file) of each of these directories, and parse them together into a pandas dataframe.我想获取每个目录的第三个文件(即 .CSV 文件),并将它们一起解析为 pandas dataframe。 This is the code I have this far这是我到目前为止的代码

import os

rootdir ='C:\\Dir\\Dir\\Dir\\root(parent)dir'
# os.listdir(rootdir)
# os.getcwd()

filelist = os.listdir(rootdir)
# file_count = len(filelist)

def list_files(dir):
    r = []
    for root, dirs, files in os.walk(dir):
        # if files.startswith('C74'):
            for name in files:
                r.append(os.path.join(root, name))
    return r

filelist = list_files(rootdir)

Now with "filelist" I get all file paths contained in all directories as strings.现在使用“filelist”,我将所有目录中包含的所有文件路径作为字符串。 Now I need to find: 1. The file names that begin with three specific letters (for example funtest, in this case the first letters being fun) 2. Take every third file, and construct a pandas dataframe from that, so that I can proceed to perform data analysis.现在我需要找到: 1. 以三个特定字母开头的文件名(例如 funtest,在这种情况下第一个字母很有趣) 2. 每隔三个文件构造一个 pandas dataframe ,这样我就可以继续进行数据分析。

IIUC we can do this much easier using a recursive function from pathlib: IIUC 我们可以使用来自 pathlib 的递归 function 更容易地做到这一点:

    from pathlib import Path
    csv = [f for f in Path(r'parent_dir').rglob('*C74*.csv')]
    df = pd.concat([pd.read_csv(f) for f in csv])

if you want to subset your list again you could do如果您想再次对列表进行子集化,您可以这样做

subset_list = [x for x in csv if 'abc' in x.stem] 

Test测试

[x for x in csv if 'abc' in x.stem]
out : ['C74_abc.csv', 'abc_C74.csv']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用python遍历目录以获取文本文件并对其进行处理 - I want to use python to walk through directories to get to text files and processed them 如果找到某些文件,如何简单地遍历目录和子目录并创建存档 - How to simply walk through directories and subdirectories and create archive if found certain files 我如何克服 Python pandas 和 matplotlib 中给定数据框的列名问题 - How can i overcome issue on column name of the given data frame in Python pandas and matplotlib 在给定特定条件的情况下,如何在 pandas dataframe 上使用 .count()? - How can I use .count() on a pandas dataframe given a certain condition? 如何以某种方式创建 pandas 数据框? - How can I create a pandas data frame in a certain way? 我可以遍历目录和子目录并将某些文件存储在数组中吗? - Can I loop through directories and subdirectories and store certain files in an array? 如何在Linux上以。开头的python中使用文件/目录? - How can I use files/directories in python on Linux that start with .? 如何向上搜索目录? 我可以 os.walk 向上到文件系统的根目录吗? - How to search upwards through directories? Can I os.walk upwards to the root of the filesystem? 你如何使用python遍历目录? - How do you walk through the directories using python? 如何使该python脚本遍历目录树? - How can I make this python script walk through a directory tree?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM