I have two dataframes and a rather tricky join to accomplish.
The first dataframe:
data = [[0, 'Standard1', [100, 101, 102]], [1, 'Standard2', [100, 102]], [2, 'Standard3', [103]]]
df1 = pd.DataFrame(data, columns = ['RuleSetID', 'RuleSetName', 'KeyWordGroupID'])
df1
Output:
RuleSetID RuleSetName KeyWordGroupID
0 Standard1 [100, 101, 102]
1 Standard2 [100, 102]
2 Standard3 [103]
... ... ...
The second one:
data = [[100, 'verahren', ['word1', 'word2']],
[101, 'flaechen', ['word3']],
[102, 'nutzung', ['word4', 'word5']],
[103, 'ort', ['word6', 'word7']]]
df2 = pd.DataFrame(data, columns = ['KeyWordGroupID', 'KeyWordGroupName', 'KeyWords'])
df2
Output:
KeyWordGroupID KeyWordGroupName KeyWords
100 verahren ['word1', 'word2']
101 flaechen ['word3']
102 nutzung ['word4', 'word5']
103 ort ['word6', 'word7']
... ... ...
The desired output:
RuleSetID RuleSetName KeyWordGroupID
0 Standard1 [['word1', 'word2'], ['word3'], ['word4', 'word5']]
1 Standard2 [['word1', 'word2'], ['word4', 'word5']]
2 Standard3 [['word6', 'word7']]
I tried to convert the second dataframe into a dictionary using df.to_dict('records') and put it into a pandas apply user defined function to match via key values but it doesn't seem like a clean approach.
Does someone has an approach to solve that? Any ideas are rewarded.