PythonPandas:如何将字符串列表与DataFrame列匹配

PythonPandas:如何将字符串列表与DataFrame列匹配,第1张

Python / Pandas:如何将字符串列表与DataFrame列匹配

这是使用个人的可读解决方案

search_func

def search_func(row):    matches = [test_value in row["Description"].lower()     for test_value in row["Text_Search"]]    if any(matches):        return "Yes"    else:        return "No"

然后按行应用此函数

# create example datadf = pd.Dataframe({"Description": ["CANSEL SURVEY E PAY", "JX154 TFR?FR xxx8690"],        "Employer": ["Cansel Survey Equipment", "Cansel Survey Equipment"]})print(df)    Description  Employer0   CANSEL SURVEY E PAY     Cansel Survey Equipment1   JX154 TFR?FR xxx8690    Cansel Survey Equipment# create text searches and match columndf["Text_Search"] = df["Employer"].str.lower().str.split()df["Match"] = df.apply(search_func, axis=1)# show resultprint(df)    Description  Employer         Text_Search          Match0   CANSEL SURVEY E PAY     Cansel Survey Equipment     [cansel, survey, equipment]     Yes1   JX154 TFR?FR xxx8690    Cansel Survey Equipment     [cansel, survey, equipment]     No


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5674869.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存