简体   繁体   English

检查具有列表值的列的元素是否存在于另一个列表中

[英]Check whether the elements of a column with list values are present in another list

I have a DataFrame with a column called antecedent (list values) and I would like to return True or False for rows where all elements are present in another list called itemset .我有一个 DataFrame 有一个名为antecedent (列表值)的列,我想为所有元素都存在于另一个名为itemset的列表中的行返回 True 或 False 。

Example itemset :示例itemset

['Investimento Fundos_commodities', 'Investimento Fundos Multimercado','Emprestimo _educacao', 'Investimento CDB']

Example antecedent :例子antecedent

['Investimento Fundos_commodities', 'Investimento Fundos']
# Desired Output : False, because we don't have the presence of the Investment Funds value in itemset.

Other Example antecedent :其他示例antecedent词:

['Investimento Fundos_commodities', 'Investimento CDB']
# Desired Output : True, because we have the presence of the two values in itemset.

I was only able to print the elements that are present in itemset , but I was not able to do this check for elements that are not present to return True or False.我只能打印itemset中存在的元素,但我无法对不存在的元素进行此检查以返回 True 或 False。

df_itemset['antecedent'].map(lambda antecedents : [x for x in antecedents if x in itemset])

# Output :
# 16     [Investimento Fundos_commodities]
# 23     [Investimento Fundos_commodities]
# 4     [Investimento Fundos_commodities]
# 26     [Investimento Fundos_commodities]
# 30     [Investimento Fundos_commodities]
                     ...                
# 138                   [Investimento CDB]
# 139                   [Investimento CDB]
# 140                   [Investimento CDB]
# 141                   [Investimento CDB]
# 142                   [Investimento CDB]
# Name: antecedent, Length: 99, dtype: object

Use set and issubset predicate:使用setissubset谓词:

data = {'antecedent': [['Investimento Fundos_commodities', 'Investimento Fundos'], 
                       ['Investimento Fundos_commodities', 'Investimento CDB']]}
df = pd.DataFrame(data)

df['issubset'] = df['antecedent'].apply(lambda x: set(x).issubset(itemset))
print(out)

# Output:
                                               antecedent  issubset
0  [Investimento Fundos_commodities, Investimento Fundos]     False
1     [Investimento Fundos_commodities, Investimento CDB]      True

You could try something like你可以尝试类似的东西

df_itemset['antecedent'].map(
  lambda antecedents : len([x for x in antecedents if x not in itemset])
) == 0

It calculates the number of antecedents which are not in itemset, so it is > 0 if and only if your condition does not hold.它计算不在项目集中的先行项的数量,因此当且仅当您的条件不成立时,它才 > 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 pandas 列中的字符串列表的元素是否存在于另一列中 - How to check if elements of a list of strings in a pandas column are present in another column 检查列表是否以另一个列表的元素开头 - Check whether a list starts with the elements of another list 检查列表元素是否存在于另一个列表的元素中 - check if element of list is present in elements of another list 检查列表的元素是否是另一个列表的元素的子集 - To check whether elements of a list is a subset of elements of another list 检查python列表中是否存在条目并添加元素 - Check whether an entry present in python list and add the elements 检查列表是否以另一个列表的元素开头,如果找到,则将两个值一起返回 - Check whether a list starts with the elements of another list and if found return both the values together 如何检查一个pandas列中列表中的所有元素是否存在于另一个pandas列中 - How to check if all the elements in list in one pandas column are present in another pandas column 检查该列表是否包含另一个列表中存在的所有类型的元素 - Check that list contains the elements of all the types present in another list 如何检查列表索引是否存在 - How to check whether a list index is present or not 检查给定列表中的元素是否存在于 DataFrame 的数组列中 - To check if elements in a given list present in array column in DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM