1 回答

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
添加回答
舉報