1

I have two pandas dataframes like below:

df1:

first_11_id spending
94838485868 50
28394958384 25
93482329832 734

df2:

first_15_id      savings
948384858684932  43
283949583845488  342
934823298324312  584

I would like to join them on id, and as you can see the first_15_id contains the first 11 digits of first_11_id

Desired output:

first_15_id      savings spending
948384858684932  43      50
283949583845488  342     25
934823298324312  584     734

Something like result = df2.join(df1, how = 'inner', on = ?)

0

1 Answer 1

2

You may

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
Sign up to request clarification or add additional context in comments.

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.