假设我有以下内容:
mylist = ["test", "new"]
df = pd.DataFrame([[["test", "whatever"]], [["tes", "test_in"]], [["new2", "new1"]]], columns=["a"])
df
a
0 [test, whatever]
1 [tes, test_in]
2 [new2, new1]
我想筛选出只包含mylist中至少一个值的行:
a
0 [test, whatever]
我无法执行以下代码:
df.query("a.str.contains('|'.join(@mylist))", engine='python')
因为这会导致部分匹配。
我在考虑以下解决方法:
df[df.apply(lambda x: set(x['a']) & set(mylist), axis=1)]
但这不起作用。