[英]Return True/False if Series of Lists contains all elements of Reference List
I have a series of lists:我有一系列列表:
s = pd.Series[(1, 2, 3), (4, 5, 6), (1, 2, 3, 4), (8, 9, 10)]
I would like to check whether any list in the series contains all elements of a reference list:我想检查该系列中的任何列表是否包含参考列表的所有元素:
l = [4, 5]
The function would return true based on the 2nd list in the series satisfying the criteria. function 将根据满足条件的系列中的第二个列表返回 true。
Ideas on how to implement this?关于如何实施的想法? I have tried the following to no avail:我尝试了以下方法无济于事:
def contains_valid_data():
return all(x in s for x in l)
def contains_valid_data():
return set(l).issubset(s)
s = pd.Series([(1, 2, 3), (4, 5, 6), (1, 2, 3, 4), (8, 9, 10)])
l = [4, 5]
def checkList(s, l):
return s[s.apply(lambda x: len(set(l).intersection(set(x)))== len(l)]
checkList(s, l)
1 (4, 5, 6)
dtype: object
I'm not sure to understand the expected output but you can try this:我不确定是否理解预期的 output 但你可以试试这个:
import pandas as pd
s = pd.Series([[1, 2, 3], [4, 5, 6], [1, 2, 3, 4], [8, 9, 10]])
l = [4, 5]
out = s.apply(lambda x: set(l).issubset(x))
print(out)
0 False
1 True
2 False
3 False
dtype: bool
Or, if you want to return a unique boolean value, you can use this:或者,如果你想返回一个唯一的 boolean 值,你可以使用这个:
def contains_valid_data():
if (s.apply(lambda x: set(l).issubset(x))==True).any():
return True
contains_valid_data()
True
You could use pandas.Series.map
to check if each row contains all the elements in l
(with a lambda expression or a function) and transform this pd.Series
into a boolean series first. You could use pandas.Series.map
to check if each row contains all the elements in l
(with a lambda expression or a function) and transform this pd.Series
into a boolean series first. And then simply use the pandas.Series.any
function to aggregate all the boolean values with an OR
operation.然后只需使用pandas.Series.any
function 通过OR
操作聚合所有 boolean 值。
# s = pd.Series([(1, 2, 3), (4, 5, 6), (1, 2, 3, 4), (8, 9, 10)])
# l = [4, 5]
l = set(l) # Transform the list into a `set` for better performance.
is_some_row_containing_l = s.map(lambda ss: set(ss).issuperset(l)).any()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.