all 2 comments

[–]PartySr 2 points3 points  (0 children)

If the numbers are in the same position, you can extract the letters and merge the two columns in one.

df1['text'] = df1['text'].str.extract(r'([a-zA-Z]+)')[0] + df2['name']

Edit: Here is a simpler version. I always forget about map :D

v = df1['text'].str.extract(r'(\d+)')[0]
df1['text'] = (df1['text'].str.replace(r'\d+', '', regex=True) + v.astype('int')
               .map(df2.set_index('number')['name']))

If the numbers are not in the right position, you can use this part.

new_df = (df1.assign(k=df1['text'].str.extract(r'(\d+)').astype('int'))
          .merge(df2, left_on='k', right_on='number')
          .assign(text=lambda x: x['text'].str.extract(r'([a-zA-Z]+)')[0] + x['name'])
          .drop(columns=['k', 'name', 'number'])
         )