【问题标题】:Reference dict variables for data manipulation purposes为数据操作目的引用 dict 变量
【发布时间】:2022-08-19 11:01:04
【问题描述】:

我已经成功地遍历了多个目录以创建 DataFrames(工作表)的列表(excel 文件)字典。然而,a) 我将如何阅读与 1-2 列表值匹配的特定工作表?并排除所有其他工作表,这样我就不会在内存中读取不必要的数据量。

sheet_list = [\"Total Residents\", \"Total (excluding Non-Residents)\", \"Individuals\", \"Corporations\", \"Other\"] 
sheet_list2 = [\"City1\", \"City2\", \"City3\", \"City4\", \"City5\", \"City6\"]

b)如何最好地引用 dict 对象值?例如,目前我的列表df_list 有 33 个元素(字典),每个字典有 14-30 个键(工作表),大多数有 360 列 x 40 行数据。我需要能够使用 list 和 dict 键按列索引值选择特定的列/行。但是,我如何知道我的列表和 dict 对象是否已以正确的顺序读入,而不可能添加额外的键/引用 ID?

例如,如果我的文件名为:1515CC, 2525CC, 3535CC, 1515DD, 2525DD, 3535DD,其中 Total Residents 表中的 1515CC 值应等于 1515DD City1 表,我需要通过拼接“N”列进行交叉检查和验证以确保它们相等或两张纸中的第 9 列并进行比较。

# Create list and iterate through select directories to get files
file_list = []
excludes = [\"graphs\", \"archive\"]
for root, directories, files in os.walk(root_path, topdown=True):
    directories[:] = [d for d in directories if d not in excludes]
    for filename in files:
        if fnmatch.fnmatch(filename, \"0*.xlsx\"):
            file_list.append(os.path.join(root,filename))

df_list = [pd.read_excel(files, sheet_name=None, skiprows=16, nrows=360, usecols=\"E:AR\") for files in file_list]

标签: python pandas list loops dictionary


【解决方案1】:

按照@srinath 的建议,我决定在根链接后面加上文件名,就像file_list.append(os.path.join(root,filename)) 一样。在我的问题中已进行了此更改,并且已修改标题以反映状态的更改。谢谢大家和@srinath。

【讨论】:

    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多