gpt4 book ai didi

python - 要索引的 id 的交叉引用列表

转载 作者:太空宇宙 更新时间:2023-11-04 04:44:41 24 4
gpt4 key购买 nike

我将与特定值相关联的 ID 列表组合在一起,并将所有这些 ID 列表放入一个数据框中。它看起来像这样:(索引 = id)

    phase  list_ids
id
a1 1 [a1,a2,c3]
a2 3 [a1,b2,c3]
b1 3 [a2,b2]
b2 2 [b1,b2,c1]
b3 3 [b2,c1]
c1 1 [a1,a2,c3]
c2 1 [a1,b1,c4]
c3 2 [c1,c2,c4]
c4 1 [c1,c2]

我想遍历这些列表并将它们交叉引用到 id 索引,其中 phase 等于 2 或 3,然后只保留原始列表中匹配的 id(或者如果不可能,创建一个修改后的新列列表)。像下面这样的东西:

    phase  list_ids
id
a1 1 [a2,c3] #ids whose phase != 2|3 not kept in list
a2 3 [b2,c3]
b1 3 [a2,b2]
b2 2 [b1,b2]
b3 3 [b2]
c1 1 [a2,c3]
c2 1 [b1]
c3 2 []
c4 1 []

如果可能的话,我想在数据框对象中执行此操作,因为每一行都有多个功能/依赖项。关于如何进行此操作的任何提示?

我的实际数据:

               phase  ids
Study_id
ACP-103-006 2.0 [ACP-103-006, ACP-103-020, ACP-103-019, ACP-10...
ACP-103-008 2.0 [ACP-103-006, ACP-103-020, ACP-103-019, ACP-10...
ACP-103-010 2.0 [ACP-103-042, ACP-103-034, ACP-103-014, ACP-10...
ACP-103-012 3.0 [ACP-103-042, ACP-103-034, ACP-103-014, ACP-10...
ACP-103-014 3.0 [ACP-103-042, ACP-103-034, ACP-103-014, ACP-10...

还有数据类型:

phase float64 
ids object
dtype: object

以及 good_ids 输出:

print(good_ids)
{'CLS1001-301', 'EFC13799', 'AG120-C-009', 'IRBES_R_04320', 'LTS11298', 'CLS1003-302', '13621', 'TMC-ORI-10-01', '11935', 'C_8428', 'ACP-103-008', 'SFY13476', 'MNTX 301EXT', '14-OBE001-016', '812P310', 'V01-126A-201', 'VX06-770-101', 'EFC11603', ...}

最佳答案

假设 list_ids 列中的每个元素都是一个字符串列表,您可以执行以下操作:

首先获取“好”ids集合(其中相位为 2 或 3):

good_ids = set(df[df["phase"].isin([2,3])].index)
print(good_ids)
#{'a2', 'b1', 'b2', 'b3', 'c3'}

接下来使用apply过滤list_ids,使用good_ids:

df["list_ids"] = df["list_ids"].apply(lambda x: [val for val in x if val in good_ids])
print(df)
# phase list_ids
#id
#a1 1 [a2, c3]
#a2 3 [b2, c3]
#b1 3 [a2, b2]
#b2 2 [b1, b2]
#b3 3 [b2]
#c1 1 [a2, c3]
#c2 1 [b1]
#c3 2 []
#c4 1 []

关于python - 要索引的 id 的交叉引用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com