I have 2 Dataframes, What I am trying to achieve to to create a list of the IDs that match. With this information I can then perform a merge later on (The merge isn't part of my question, I just want to find out how to match the rows correctly).
DF1
| ID |
|---|
| 123 |
| 123B |
| 234 |
| 234B |
| 456 |
| 456B |
| asfdh5 |
| 45gh |
DF2
| ID |
|---|
| 123456 |
| 123B456 |
| BBBER4 |
| 234567YT |
| B9234BAA |
| 456XXA32 |
| 999GHF |
| pp9985 |
| 88AVKVBD |
Desired Result - I want to get a list of string that match
| Matches | |
|---|---|
| 0 | 123 |
| 1 | 123B |
| 2 | NaN |
| 3 | 234 |
| 4 | 234B |
| 5 | 456 |
| 6 | NaN |
| 7 | NaN |
| 8 | NaN |
The current code I am using is the following
df2['ID'].str.extract(fr"({'|'.join(df1['ID'].values)})", expand=False)
The issue is that this code doesn't match the IDs in DF1 exactly, if you see for index 2 in Desired Results above the result is 123B, However I'm only getting 123.
Below are the results I'm getting with my current code.
| Matches | |
|---|---|
| 0 | 123 |
| 1 | 123 |
| 2 | NaN |
| 3 | 234 |
| 4 | 234 |
| 5 | 456 |
| 6 | NaN |
| 7 | NaN |
| 8 | NaN |
Any help would be much appreciated. Thanking you, Cian