您好问题是,通过搜索单词列表来遍历一列,然后如果找到任何搜索到的列表单词,则创建一个布尔列。 这是我的代码
# 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,但实际上有两个以上。我的代码有什么问题? 谢谢
答案 0 :(得分:2)
您的问题是您在$DIR_LIST
中的每个df_codeNaf['topNAF']
中都更改了banch
。您的代码可以通过以下方式固定:
code
但是更好的是,您可以在一行中将df_codeNaf['topNAF'] = False
for branch in code:
df_codeNaf['topNAF'] = df_codeNaf['topNAF'] | df_codeNaf['Code_NAF'].str.contains(branch).
与regex
一起尝试:
contains
答案 1 :(得分:0)
这是使用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)