我有这个 df:
pd.DataFrame([[1, "type_1"], [2, "type_2"], [2, "type_1; type_2"], [2, "type_1; type_3"], [2, "type_3"], [2, "type_1; type_2, type_3"]],
columns=["a", "b"])
a b
0 1 type_1
1 2 type_2
2 2 type_1; type_2
3 2 type_1; type_3
4 2 type_3
5 2 type_1; type_2, type_3
我需要使用从配置文件中获取的大量查询字符串,如下所示:
my_list = ["type_1", "type_2"]
df.query("a == 2 and b in @my_list")
现在输出:
a b
1 2 type_2
但我希望输出是这样的,因为 b 中至少有一个值在 my_list 中:
a b
0 2 type_2
1 2 type_1; type_2
2 2 type_1; type_3
3 2 type_1; type_2, type_3
如您所见,问题是我的某些列实际上是列表。目前它们是由 分隔的字符串,;但我可以将它们转换为列表。但是,我不确定这将如何帮助我仅使用 .query()从column b内部过滤具有至少一个值的行(因为否则我将不得不解析查询字符串并且它会变得混乱)my_list
这将是列表的等效代码:
pd.DataFrame([[1, ["type_1"]], [2, ["type_2"]], [2, ["type_1", "type_2"]], [2, ["type_1", "type_3"]], [2, "type_3"], [2, ["type_1", "type_2", "type_3"]]],
columns=["a", "b"])
梦里花落0921
鸿蒙传说
相关分类