繁体   English   中英

从正在检查列表的字符串 findall output 中删除方括号

[英]Remove square brackets from string findall output that is checking a list

我有一个看起来像这样的df:

    import pandas as pd
    import numpy as np

print(df)

       Items
    0  Product A + Product B + Product C   
    1  Product A + Product B + Product B1 + Product C1 

我正在使用以下代码查看列中包含的项目是否包含在列表中:

My_Items = ['Product B1', 'Product C']

Item_mask = df.Items.str.findall('|'.join(My_Items )).str.len()
df['Item_list'] = df.Items.str.findall('|'.join(My_Items))

这给了我一个看起来像这样的新列:

   Items                                                 Item_list
0  Product A + Product B + Product C                     [Product C]
1  Product A + Product B + Product B1 + Product C1       [Product B1]

有谁知道我怎样才能让项目列表只给我我正在搜索的项目而没有 [] 括号?

所需的 output 如下:

   Items                                                 Item_list
0  Product A + Product B + Product C                     Product C
1  Product A + Product B + Product B1 + Product C1       Product B1

我尝试使用以下方法将其转换为字符串:

df['Item_list'] = df.Items.str.findall('|'.join(My_Items)).astype(str)

但这给了我这样的数据,例如 ['Product C'],这也不是我想要的。

我也尝试了一个 iterrows 解决方案,它给了我我想要的 output 但完成时间太长了,真正的数据源非常大!

任何帮助/指导将不胜感激!

亲切的问候

只需将.apply(','.join)添加到您的findall命令中,如下所示:

df['Item_list'] = df.Items.str.findall('|'.join(My_Items)).apply(','.join)                                                                                                                        

Output:

                                             Items             Item_list
0                Product A + Product B + Product C             Product C
1  Product A + Product B + Product B1 + Product C1             Product B1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM