3 回答
TA贡献2021条经验 获得超8个赞
您很接近,仅将空集的集转换为 bool 到False,否则True:
df = df[df['a'].apply(lambda x: bool(set(x) & set(mylist)))]
print (df)
a
0 [test, whatever]
选择:
df = df[[bool(set(x) & set(mylist)) for x in df['a']]]
或者:
df = df[[bool(set(x).intersection(mylist)) for x in df['a']]]
TA贡献1825条经验 获得超6个赞
isin重新创建列表列后检查
df[pd.DataFrame(df.a.tolist()).isin(mylist).any(1)]
Out[23]:
a
0 [test, whatever]
TA贡献1835条经验 获得超7个赞
这对我有用:
mylist = ["test", "new"]
df = pd.DataFrame([[["test", "whatever"]], [["tes", "test_in"]], [["new2", "new1"]]], columns=["a"])
print(df)
def func(x):
for e in x[0]:
if(e in mylist):
return True
else:
continue
return False
df = df.loc[df.apply(lambda x: func(x), axis=1), :]
print(df)
添加回答
举报