0

I have two dataframes A and B. Dataframe A looks like:

col1    col2   col3
 a_low   5      6
 a_low   3      10
 a_high  4      4

Dataframe B looks like:

col1 colB 
 a    90

Now, I want to merge df A and B on the substring a in col1 from df A and col1 from df B. Hence, the result should be:

col1    col2   col3  colB
 a_low   5      6     90
 a_low   3      10    90
 a_high  4      4     90

Anyone knows how to do this using merge?

3 Answers 3

2

You can also use assign() and merge() method:

result=dfA.assign(col1=dfA['col1'].str.split('_').str[0]).merge(dfB).assign(col1=dfA['col1'])

Now if you print result you will get your desired output:

    col1    col2    col3    colB
0   a_low   5       6       90
1   a_low   3       10      90
2   a_high  4       4       90
Sign up to request clarification or add additional context in comments.

Comments

2

You need to extract the part of string from col1, e.g. with str.split or str.extract(), then either merge or map:

dfA['colB'] = (dfA['col1'].str.split('_').str[0]
                  .map(dfB.set_index('col1')['colB']
              )

Comments

1

You can use str.extract + insert:

pat = "|".join(df_a.col1)
df_b.insert(0, 'a', df_b['col1'].str.extract("(" + pat + ')', expand=False))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.