【发布时间】:2019-07-12 08:28:55
【问题描述】:
我有 2 个数据框,我想将它们合并到一个公共列上。但是,我想合并的列不是同一个字符串,而是一个字符串包含在另一个字符串中:
import pandas as pd
df1 = pd.DataFrame({'column_a':['John','Michael','Dan','George', 'Adam'], 'column_common':['code','other','ome','no match','word']})
df2 = pd.DataFrame({'column_b':['Smith','Cohen','Moore','K', 'Faber'], 'column_common':['some string','other string','some code','this code','word']})
我希望d1.merge(d2, ...) 得到的结果如下:
column_a | column_b
----------------------
John | Moore <- merged on 'code' contained in 'some code'
Michael | Cohen <- merged on 'other' contained in 'other string'
Dan | Smith <- merged on 'ome' contained in 'some string'
George | n/a
Adam | Faber <- merged on 'word' contained in 'word'
【问题讨论】:
-
为什么
'John'不匹配K因为'this code' -
@ALollz 那是因为“一些代码”是第一位的