【发布时间】:2021-01-14 09:46:40
【问题描述】:
我有一个数据框,基于名为“originator”的列中的字符串,我想检查该字符串是否包含位于另一个列表中的单词。如果字符串中有一个单词位于所述列表中,则将列 originator_prediction 更新为“org”。
有没有更好的方法来做到这一点?我是按以下方式完成的,但速度很慢。
for row in df['ORIGINATOR'][1:]:
string = str(row)
splits = string.split()
for word in splits:
if word in COMMON_ORG_UNIGRAMS_LIST:
df['ORGINATOR_PREDICTION'] = 'Org'
else:
continue
df = pd.DataFrame({'ORIGINATOR': ['JOHN DOE', 'APPLE INC', 'MIKE LOWRY'],
'ORGINATOR_PREDICTION': ['Person', 'Person','Person']})
COMMON_ORG_UNIGRAMS_LIST = ['INC','LLC','LP']
具体来说,如果您查看我们的数据框“APPLE INC”中的第 2 行,应该有一个 originator_prediction = 'ORG' 而不是 person。
原因是,我们遍历了我们常见的 org unigrams 列表,其中 INC 一词。
【问题讨论】:
-
我会说是的,你能发布一些示例数据和预期输出吗?
-
Scott 在问题中添加了更多信息
标签: python pandas nested-loops