I have 2 dataframes:
df1=pd.DataFrame({'number': ['14578', '45621', '1564']})
df2=pd.DataFrame({'number': ['1457891521', '123456215', '15643']})
My question how is it possible to determine if df1['number'] contains in df2['number'] strictly from the left.
Desirable result:
number full number
0 14578 1457891521
1 45621 0
2 1564 15643
CodePudding user response:
You unfortunately need to loop here:
df1['full number'] = [b if b.startswith(a) else ''
for a,b in zip(df1['number'], df2['number'])]
Output:
number full number
0 14578 1457891521
1 45621
2 1564 15643
CodePudding user response:
Another possible solution, using numpy
, specifically numpy.char.startswith
:
x2, x1 = df2['number'].values.astype(str), df1['number'].values.astype(str)
out = df1
out['full number'] = df2.loc[np.char.startswith(x2, x1), 'number']
Output:
number full number
0 14578 1457891521
1 45621 NaN
2 1564 15643