繁体   English   中英

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

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

我有一个 DataFrame 有一个名为antecedent (列表值)的列,我想为所有元素都存在于另一个名为itemset的列表中的行返回 True 或 False 。

示例itemset

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

例子antecedent

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

其他示例antecedent词:

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

我只能打印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

使用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

你可以尝试类似的东西

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

它计算不在项目集中的先行项的数量,因此当且仅当您的条件不成立时,它才 > 0。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM