[英]Pandas if column contains string then write to second dataframe
我有兩個數據框。 我希望能夠從 DF2 中搜索列(snippet_matched),看看它是否與 DF1 中的列(搜索)中的任何內容部分匹配。 如果它確實包含它,那么我想在 DF1 列(HR)中寫“是”。 這樣的事情可能嗎?
df1
Number Search HRC
1 1 test game
2 2 test game
3 3 test game
df2
Title Snipped_Match
User Win
Sony 1 game
預期的
Number Search HRC
1 1 test game Yes
2 2 test game Yes
3 3 test game Yes
您可以為部分匹配創建 function 並將其應用於 df1['Search'] 列。 見下文:
def match(cell, column):
column=[set(i.split()) for i in column]
cell=set(cell.split())
return 'Yes' if any(k-cell==set() for k in column) else 'No'
df1['HR']=df1['Search'].apply(lambda x: match(x, df2['Snipped_Match']))
結果:
>>> print(df1)
Number Search HR
0 1 1 test game Yes
1 2 2 test game No
2 3 3 test game No
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.