I have two dataframes df1 and df2:
df1 = pd.DataFrame({'a':['123456','123457', '23456', '23457', '345678','345679'],
'b':['e','f','g','h','i','j']})
df2 = pd.DataFrame({'id':['2', '123', '3456'],
'b1':['c1','c2','c3']})
ID b1
2 c1
123 c2
3456 c3
a b
123456 e
123457 f
23456 g
23457 h
456789 i
456789 j
What I want to create:
df3 = pd.DataFrame({'a':['123456','123457', '23456', '23457', '345678','345679'],
'b':['e','f','g','h','i','j'],
'id':['123','123','2','2','3456','3456'],
'b1':['c2','c2','c1','c1','c3','c3']})
a b id b1
123456 e 123 c2
123457 f 123 c2
23456 g 2 c1
23457 h 2 c1
456789 i 4567 c3
456789 j 4567 c3
How can I merge the data from df2 into df1 based on the 'a1' match gaven the substring in 'a' for chars 0-N(first N charcters, N based on the length of string in 'a1').