5

I run Python 3.6 on Windows 10.

My code is the following:

data1
     Loan_ID    Gender
1   LP001003    Male
2   LP001005    Male
3   LP001006    Male
4   LP001008    Male
5   LP001011    Male

data2
    Loan_ID2    LoanAmount
1   LP001003    128.0
2   LP001005    66.0
3   LP001006    120.0
4   LP001008    141.0
5   LP001011    267.0

data_merged = data1.merge(right= data2, how='inner',left_on='Loan_ID', right_on = 'Loan_ID2',right_index=True, sort=False)

data_merged.shape
(0, 4)

3 Answers 3

3

You do not need right_index within merge

df1.merge(df2,left_on='Loan_ID',right_on='Loan_ID2')
Out[54]: 
    Loan_ID Gender  Loan_ID2  LoanAmount
0  LP001003   Male  LP001003       128.0
1  LP001005   Male  LP001005        66.0
2  LP001006   Male  LP001006       120.0
3  LP001008   Male  LP001008       141.0
4  LP001011   Male  LP001011       267.0
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please explain why the right_index created a problem?
@user8270077 when you are doing the merge and type right_index you are Use the index from the left DataFrame as the join key(s).
2

Try renaming your column in data2, this'll simplify your merge code a bit.

data1.merge(data2.rename(columns={'Loan_ID2' : 'Loan_ID'}), on='Loan_ID')

    Loan_ID Gender  LoanAmount
0  LP001003   Male       128.0
1  LP001005   Male        66.0
2  LP001006   Male       120.0
3  LP001008   Male       141.0
4  LP001011   Male       267.0

Comments

0

Just remove the right_index part:

data_merged = pd.merge(data1, data2, how='inner',left_on='Loan_ID', right_on = 'Loan_ID2', sort=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.