将多个单列 excel 文件与特定的嵌套列表/元组进行比较

Comparing a number of single column excel files to specific nested list / tuples

我正在寻求一些建议。 我有一个包含 30 个嵌套元素的元组(从 json 响应转换而来) 采用这种格式:

[('Group_1',['xyz123','str123','834hsj','nmp001','888tyu','abc123']),...('Group_30' ,['aaaa', 'bbb', 'fff'])

我有 5 个 excel 文件以元组中的 5 个对应组加上它们的行命名如下:

Excel xls 文件 1: 姓名:Group_1 内容:

Column: A
Row1: Group_1
Row2: xyz123
Row3: str123
Row4: 834hsj
Row5: nmp001
Row6: 888tyu
Row7: abc123

Excel xls 文件 2: 姓名:Group_2 内容:

Row1: Group_2

等直到 Group_5

目的是比较元组和excel文件中元素的组匹配值,使元组加嵌套列表中的Group_1到Group_5匹配[的内容=35=] 及其列内容。如果对应组有差异,则列出缺失或未完成的字符串及其位置。

您是否建议将 excel 文件(它们都是 1 列,大小不同,长度不同)作为单独的数据帧导入 panda,并将元组分解为单独的列表,然后也将其分解为 panda 数据帧?或者在数据框中导入 excel 然后转换成列表(每组 1 个)以与元组进行比较(必须将其分成组列表。

谢谢

最简单的方法是循环读取每个文件,将每个列表变成一个集合, 和 get wild.:

假设您在列表中有您的元组列表 groups:

groups

[('Group_1',['xyz123','str123','834hsj','nmp001','888tyu','abc123']),
 ('Group_30' ,['aaaa', 'bbb', 'fff'])]

并且您的文件以这样的组名命名:

Group_1.xls
Group_30.xls

首先,读入XLS,跳过第一行(即'A'),将第二行设置为列名(即'Group_1')。

for group in groups:
    df = pd.read_excel(group[0] + '.xls', header=0, skiprows=[0])

它应该是这样的:

df

  Group_1
0  xyz123
1  str123
2  834hsj
3  nmp001
4  888tyu
5  abc123

然后,我们将文件和列表中的元素转化为集合并输出结果:

for group in groups:
    df = pd.read_excel(group[0] + '.xls', header=0, skiprows=[0])

    file_set = set(df[group[0]].to_list())
    tup_set = set(group[1])

    print()
    print("In file and in tuple")
    print(file_set.intersection(tup_set))
    print("In file, but not in tuple")
    print(file_set.difference(tup_set))
    print("In tuple, but not in file")
    print(tup_set.difference(file_set))

你应该得到这样的输出:

In file and in tuple
{'nmp001', '834hsj', '888tyu', 'str123', 'abc123', 'xyz123'}
In file, but not in tuple
set()
In tuple, but not in file
set()

In file and in tuple
set()
In file, but not in tuple
{'nmp001', '834hsj', '888tyu', 'str123', 'abc123', 'xyz123'}
In tuple, but not in file
{'bbb', 'fff', 'aaaa'}

PS。 set() 是空集。