[英]Compare each pandas row to a dictionary of list and append new variable to the dataframe
我想檢查 pandas dataframe 字符串列的每一行和 append 如果在字典中找到文本列表的任何元素,則返回 1 的新列。
例子:
# Data
df = pd.DataFrame({'id': [1, 2, 3],
'text': ['This sentence may contain reference.',
'Orange, blue cow','Does the cow operate any heavy machinery?']},
columns=['numbers', 'text'])
# Rule dictionary
rule_dict = {'rule1': ['Does', 'the'],
'rule2':['Sentence','contain'],
'rule3': ['any', 'reference', 'words']}
# List of variable names to be appended to df
rule_list = ['has_rule1','has_rule2','has_rule3']
# Current for loop
for Key in rule_dict:
for i in rule_list:
df[i] = df.text.apply(lambda x: (
1 if any(ele in x for ele in rule_dict[Key]) == 1 and (len(str(x)) >= 3)
else 0))
# Current output, looks to be returning a 1 if text is found in ANY of the lists
df = pd.DataFrame({'id': [1, 2, 3],
'text': ['This sentence may contain reference.',
'Orange, blue cow','Does the cow operate any heavy machinery?'],
'has_rule1': [1,1,1],
'has_rule2': [0,0,0],
'has_rule3': [1,1,1]},
columns=['id', 'text','has_rule1','has_rule2','has_rule3'])
# Anticipated output
df = pd.DataFrame({'id': [1, 2, 3],
'text': ['This sentence may contain reference.',
'Orange, blue cow','Does the cow operate any heavy machinery?'],
'has_rule1': [0,0,1],
'has_rule2': [1,0,0],
'has_rule3': [1,0,1]},
columns=['id', 'text','has_rule1','has_rule2','has_rule3'])
假設您解決了評論中提到的有關 dict 理解的問題,則不應使用嵌套for
循環。 相反,使用帶有zip
的單個for
循環:
for (k,v), n in zip(rule_dict.items(), rule_list):
pat = rf'\b{"|".join(v)}\b'
df[n] = df.text.str.contains(pat).astype(int)
Output:
id text has_rule1 has_rule2 has_rule3
-- ---- ----------------------------------------- ----------- ----------- -----------
0 1 This sentence may contain reference. 0 1 1
1 2 Orange, blue cow 0 0 0
2 3 Does the cow operate any heavy machinery? 1 0 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.