merge在pandas中返回空数据帧

时间:2018-05-01 15:21:16

标签: python pandas merge

我在Windows 10上运行Python 3.6。

我的代码如下:

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 个答案:

答案 0 :(得分:3)

right_index

中您不需要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

答案 1 :(得分:2)

尝试在data2中重命名您的列,这会简化您的合并代码。

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

答案 2 :(得分:0)

只需删除right_index部分:

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