I want to compare the col1 and the col2 which one is matched in partial. I wrote the code like below, but the first row show 'Misamtch' not 'Match'
import pandas as pd
data_in = {'col1': ['BANQ1049576495', 'HLCUSEL221162979', 'SEL1469779'],
'col2': ['KNKX1049576495', 'SEL221162979', 'KROL1020107403']}
df_input = pd.DataFrame(data_in)
data_out = {'col1': ['BANQ1049576495','HLCUSEL221162979','SEL1469779'],
'col2': ['KNKX1049576495','SEL221162979','KROL1020107403'],
'col3':['Match','Match','Mismatch']}
df_ouput = pd.DataFrame(data_out)
def compare_func(row):
pattern = re.compile('.*' + re.escape(row['col1']) + '.*')
if re.search(pattern, row['col2']):
return 'Match'
else:
return 'Mismatch'
data_in['col3'] = data_in.apply(compare_func, axis=1)
data_ouput = data_in
Can you Please fit the code so that even if the values in two columns are partially matched, it will be checked as a match?