Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge returns empty dataframe in pandas

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)
like image 248
user8270077 Avatar asked May 01 '18 15:05

user8270077


2 Answers

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
like image 145
BENY Avatar answered Oct 04 '22 01:10

BENY


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
like image 39
cs95 Avatar answered Oct 04 '22 03:10

cs95