我有两个如下所示的熊猫数据框:
df1:
first_11_id spending
94838485868 50
28394958384 25
93482329832 734
df2:
first_15_id savings
948384858684932 43
283949583845488 342
934823298324312 584
我想加入他们的id,如您所见,first_15_id
包含first_11_id
的前11位数字
所需的输出:
first_15_id savings spending
948384858684932 43 50
283949583845488 342 25
934823298324312 584 734
类似result = df2.join(df1, how = 'inner', on = ?)
答案 0 :(得分:1)
您可以
df2.assign(first_11_id=df2.first_15_id.astype(str).str[:11].astype(float))\
.merge(df, on='first_11_id')\
.drop('first_11_id', 1)
first_15_id savings spending
0 948384858684932 43 50
1 283949583845488 342 25
2 934823298324312 584 734