简体   繁体   中英

Partial String Merge on Pandas Dataframe

I have two dataframes with varying information, but both dataframes have an account_number column that I was hoping to do a merge with.

The problem lies in this:

One of the dataframes has the full account numbers, for example 12345678 and the other dataframe has masked account numbers except for the last four digits so it would be like ****5678 .

I know for sure that all the last four digits are different for each account number, so how would I go about merging the dataframes together in an inner join if the last four digits match?

Thank you for all your help.

I am assuming these columns are in the string format, if they are not, please update your question to specify that.

If you are sure the last four digits will be unique, I would create a new column with the last four, and merge on that. This can be done by using map and lambda .

df1['last_four'] = df1['account_number'].map(lambda x: x[-4:])
df2['last_four'] = df2['account_number'].map(lambda x: x[-4:])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM