1 回答
![?](http://img1.sycdn.imooc.com/54584cde0001d19202200220-100-100.jpg)
TA贡献1891条经验 获得超3个赞
通过反转给定的键值对来创建替换dict
字典,即对于列表中的每个值将其映射到其相应的键,然后使用Series.replace
替换组合列中的字符串Line Item
以及Insertion Order
替换字典中的相应值(当存在时)匹配,最后mask
是无法替换的字符串:
r = {rf'(?i).*?\b{z}\b.*':x for x, y in dct.items() for z in y}
s = df['Line Item'].add(':' + df['Insertion Order'])
df['Creative Type'] = s.replace(r, regex=True).mask(lambda x: x.eq(s))
Line Item Insertion Order Creative Type
1 blbl 33 dEs '300x600' Q3 hello 444 RISING STARS
2 QQQ4 Hello trueview Apple something 68793274 VIDEO
3 A useless string pre-roll Video <10 tttt 89 CASIO VIDEO
4 Something not in dict Neither here NaN
添加回答
举报