假設這是我的數據框:dw = {'id' : [1,2,3,4,5], 'first_item' : [['Motherboard', 'Miscellaneous'], ['Miscellaneous', 'Mechanical Hardware'], ['Motherboard', 'Hard Drive'], ['Mechanical Hardware', 'Hard Drive'], ['Motherboard','Mechanical Hardware']], 'second_item' : [['Motherboard', 'Hard Drive'], ['Mechanical Hardware', 'Mechanical Hardware'], ['Motherboard', 'Hard Drive'], ['Mechanical Hardware', 'Hard Drive'], ['Motherboard','Miscellaneous']]}dw = pd.DataFrame(dw)我想找到第一項和第二項之間的交集/公共元素(按行),得到如下輸出: dw['new']1 ['Motherboard']2 ['Mechanical Hardware']3 ['Motherboard', 'Hard Drive']4 ['Mechanical Hardware', 'Hard Drive']5 ['Motherboard']我已經嘗試過下面的代碼,但它沒有產生預期的結果:def intersection(lst1, lst2): return list(set(lst1) & set(lst2))dw['new'] = dw.apply(lambda x: intersection(dw.first_item, dw.second_item), axis = 1)
添加回答
舉報
0/150
提交
取消