When joining between python daframes, is it possible to match the included key value instead of the same key value?

Asked 2 years ago, Updated 2 years ago, 351 views

When joining between python daframes, is it possible to match the included key value instead of the same key value?

For example,

DF_A present and

DF_B is present.

What I want to do is combine two data frames based on column A and column F At this time, the first row of A is ABC, and the first row of F is BC, and although it is not the same key value, is there a way to match and combine it when it meets the conditions as an inclusion relationship?

Below is the data frame that I want.

We transformed each data frame into a dictionary form and matched it through KEY's inclusion to combine the data frames, but all of the above column names were not maintained, so we are trying the method mentioned above.

If the method I mentioned is not possible, is there a way to combine the two data frames based on the inclusion relationship between the column values while maintaining the column?

python dataframe join pandas

2022-10-21 00:00

1 Answers

https://towardsdatascience.com/joining-dataframes-by-substring-match-with-python-pandas-8fcde5b03933

I don't think there is a function that supports directly from join or merge of the Pandas data frame. After a little googling, the above document comes out. (I searched for Pandas join with key substring relationship. )

It's a code that does exactly what the questioner wants. You can read the blog post for a description of the code.

First of all, we find (1) all combinations with the cartesian product of the two df, calculate whether the column (2) satisfies the relationship of the columns in the entire df containing all the columns, create one match column, and (3) filter only those with the match column true.


2022-10-21 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.