0

I have two pandas dataframe that I am trying to join. The key in df1 is a substring of key in df2. Here's sample data for Illustration.

import pandas as pd

df1 = pd.DataFrame({
                    'id': [22380, 10380, 22380],
                    'n': ['A', 'B', 'C']
                  })

df2 = pd.DataFrame({
                    'id': ['SMS04223800000000001', 'SMS04103800000000001', 'SMS04223800000000001'],
                    'v': [10, 12, 15]

                   })
              })

Expected output after merging:

id                      n     v

SMS04223800000000001    A     10
SMS04103800000000001    B     12
SMS04223800000000001    C     15
kms
  • 1,810
  • 1
  • 41
  • 92
  • 1
    If this is the pattern in every row, you can first get the substring from `df2` by doing `df2.id.str[5:10]`. Then do the merging on this – T C Molenaar Sep 30 '22 at 08:09

0 Answers0