Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 遍历两列以查看列表中是否存在子字符串,然后将其添加到第三列_Python_Regex_Pandas_Dataframe - Fatal编程技术网

Python 遍历两列以查看列表中是否存在子字符串,然后将其添加到第三列

Python 遍历两列以查看列表中是否存在子字符串,然后将其添加到第三列,python,regex,pandas,dataframe,Python,Regex,Pandas,Dataframe,我有一个字符串列表: YOUTUBE = ['bumper youtube','yt trueview', 'youtube trueview','yt pre-roll','youtube pre-roll','yt bumper','youtube bumper' ,'bumper','yt preferred pre-roll','youtube preferred pre-roll', 'YT preferred bumper', 'youtube preferred bumper',

我有一个字符串列表:

YOUTUBE = ['bumper youtube','yt trueview', 'youtube trueview','yt pre-roll','youtube pre-roll','yt bumper','youtube bumper' ,'bumper','yt preferred pre-roll','youtube preferred pre-roll', 'YT preferred bumper', 'youtube preferred bumper', 'YT masthead', 'youtube masthead', 'trueview youtube','trueview']
我还有一个熊猫数据框,如下所示:

Line Item                                      |    Insertion Order            | Creative Size           
_____________________________________________________________________
ch video cross ff bumper youtube mk it mb      | gen 20 youtube                | Unknown    
moisturizerbody trueview ym21998557 yt youtube | trueview ym21998557 yt youtube| Unknown
useless string                                 | dunno                         | Unknown
012020 trueview ym21978191 yt youtube bumper   | davena bumper youtube 24      | Unknown
我必须迭代列插入顺序和创造性大小。 如果列表中的任何值位于列行项目或插入顺序中,则列表中的字符串应以创造性大小插入到相应行中。如果存在多个可能的匹配项,则任何匹配的字符串都可以。 我怎么做?如有必要,可以导入正则表达式

通过
+
\b\b
将两个字符串列连接起来,用于单词边界:

pat = '|'.join(r"\b{}\b".format(x) for x in YOUTUBE)
df['new'] = (df['Line Item'] + ' ' + df['Insertion Order']).str.findall(pat).str.join(', ')

print (df)
                                        Line Item  \
0       ch video cross ff bumper youtube mk it mb   
1  moisturizerbody trueview ym21998557 yt youtube   
2                                  useless string   
3    012020 trueview ym21978191 yt youtube bumper   

                  Insertion Order Creative Size                         new  
0                  gen 20 youtube       Unknown            [bumper youtube]  
1  trueview ym21998557 yt youtube       Unknown                  [trueview]  
2                           dunno       Unknown                          []  
3        davena bumper youtube 24       Unknown  [trueview, bumper youtube]  
如果NEE通过
连接字符串,
添加:


这个实现很简单,应该可以工作。最后一个匹配项(在“行项目”或“插入顺序”列中)将覆盖任何以前的匹配项(在这两列中),此字符串是您将在每行的“创造性大小”列中看到的字符串。df_ex是示例数据帧的名称

for idx,row in df_ex.iterrows():
    for string in YOUTUBE:
        if (string in row['Line Item']) or (string in row['Insertion Order']): # can also use regex here
            df_ex.loc[idx, 'Creative Size'] = string

您可以找到存在匹配项的索引,并将该匹配项复制到感兴趣的列,如本例所示:

data = pd.DataFrame({"Line Item": ['nope', 'nope', 'yep1!'],"Insertion Order": ['nope', 'yep2!', 'nope'], "Creative Size": ['', '', '']})
youtube = ['foo', 'bar', 'yep1!', 'yep2!']

for col in data.columns:
    ind = data.loc[data[col].isin(youtube)].index
    data['Creative Size'].iloc[ind] = data[col].iloc[ind]

data
输出:

由于某些原因,它不起作用,可能是由于SettingWithCopyWarning:试图在数据帧切片的副本上设置值。尝试改用.loc[row\u indexer,col\u indexer]=value?完整的代码是什么?是否可能存在编辑问题?欢迎使用堆栈溢出。您可能希望通过添加对关键部分的简短解释来改进您的答案。对于未来的读者来说,更有趣的是看到解释为什么这会回答这个问题。
data = pd.DataFrame({"Line Item": ['nope', 'nope', 'yep1!'],"Insertion Order": ['nope', 'yep2!', 'nope'], "Creative Size": ['', '', '']})
youtube = ['foo', 'bar', 'yep1!', 'yep2!']

for col in data.columns:
    ind = data.loc[data[col].isin(youtube)].index
    data['Creative Size'].iloc[ind] = data[col].iloc[ind]

data