Python从lis中搜索数据帧中的字符串

2024-04-26 13:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下清单:

search_list = ['STEEL','IRON','GOLD','SILVER']

我需要在数据帧(df)中搜索:

^{pr2}$

并将匹配的行插入到新的数据帧(newdf)中,从列表中添加具有匹配词的新列:

      a    b                   c
0    123   'Blah Blah Steel'   'STEEL'
1    789   'Blah Blah Gold'    'GOLD'

我可以使用以下代码提取匹配行:

newdf=df[df['b'].str.upper().str.contains('|'.join(search_list),na=False)]

但我不知道如何将列表中匹配的单词添加到c列中

我在想,匹配需要捕获列表中匹配词的索引,然后使用索引号来提取值,但我不知道如何做到这一点。在

如有任何帮助或建议,我们将不胜感激

谢谢


Tags: 数据df列表searchsilverlistblahsteel
3条回答

一种方法是

def get_word(my_string):
    for word in search_list:
         if word.lower() in my_string.lower():
               return word
    return None

new_df["c"]= new_df["b"].apply(get_word)

你也可以按照

^{pr2}$

对于第一个,您可以选择先将列c添加到df,然后过滤掉{},而如果{}不包含任何单词,第二个将抛出错误。在

你也可以看到这个问题:Get the first item from an iterable that matches a condition

从最高分的答案中运用这个方法

new_df["c"]= new_df["b"].apply(lambda my_string: next(word for word in search_list if word.lower() in my_string.lower())

您可以使用extract并过滤掉那些nan(即不匹配):

search_list = ['STEEL','IRON','GOLD','SILVER']

df['c'] = df.b.str.extract('({0})'.format('|'.join(search_list)), flags=re.IGNORECASE)
result = df[~pd.isna(df.c)]

print(result)

输出

^{pr2}$

请注意,您必须导入re模块才能使用re.IGNORECASE标志。作为替代,您可以直接使用2,这是re.IGNORECASE标志的值。在

更新

如@user3483203所述,您可以使用以下方法保存导入:

df['c'] = df.b.str.extract('(?i)({0})'.format('|'.join(search_list)))

您可以使用set.intersection查找列b中出现的单词:

search_list = set(['STEEL','IRON','GOLD','SILVER'])
df['c'] = df['b'].apply(lambda x: set.intersection(set(x.upper().split(' ')), search_list))

输出:

^{pr2}$

如果要删除没有匹配项的行,请使用df[df['c'].astype(bool)]

     a                b        c
0  123  Blah Blah Steel  {STEEL}
2  789   Blah Blah Gold   {GOLD}

相关问题 更多 >