在列中搜索单词列表,如果找到单词,则创建一个布尔列 [英] Search in column a list of word and create a boolean column if a word is found

查看:72
本文介绍了在列中搜索单词列表,如果找到单词,则创建一个布尔列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好问题是,通过搜索单词列表来遍历一列,如果找到了搜索到的列表单词中的任何一个,则创建一个布尔列. 这是我的代码

Hello problem to loop over a column by searching a list of word then create a Boolean column if any of the list word searched is found. Here is my code

# Code naf related to sport.
code = ["3230Z","4764Z","7721Z","8551Z","9311Z", "9312Z", "9313Z", "9319Z",
        "9329Z", "364Z" "524W", "714B", "804C", "926A", "926C", "930L", "927C",
        "923K"]

# check keywords of code into "Code_Naf" column
for branch in code:
    df_codeNaf["topNAF"] = df_codeNaf["Code_NAF"].str.contains("3230Z" or "4764Z" or "7721Z" or "8551Z"
                                                                      or "9311Z" or "9312Z" or "9313Z" or "9319Z"
                                                                      or "9329Z" or "364Z" "524W" or "714B" or
                                                                      "804C" or "926A" or "926C" or "930L" or
                                                                      "927C" or "923K")

当我在topNaf栏中查看时,我发现只有2个True,但实际上有两个以上.我的代码有什么问题? 谢谢

When I look in the topNaf column I found only 2 True but in reality there more than two. What's wrong with my code? Thanks

推荐答案

以下是使用lambda的方法

Here a method using lambda

code = ["3230Z","4764Z","7721Z","8551Z","9311Z", "9312Z", "9313Z", "9319Z",
        "9329Z", "364Z" "524W", "714B", "804C", "926A", "926C", "930L", "927C",
        "923K"]

df_codeNaf["topNAF"] = df_codeNaf["Code_NAF"].apply(lambda x: True if x in code else False)

这篇关于在列中搜索单词列表,如果找到单词,则创建一个布尔列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆