I have two sample data frames:
df1 = pd.DataFrame({'Model': ['0RW52HC5KDD13R', '0RW52HC5KDD13U','JJS42HC5JSSAYR']})
df2 = pd.DataFrame({'Group_Var': ['0RW52HC5K', '0RW52HC5K','JJS42HC5J']})
Using this will result result in an empty dataframe.
df3 = df1.merge(df2, left_on='Model', right_on='Group_Var')
How could I go about using a merge to use a substring / partial match from df2['Group_Var']
in df1['Model']
? Perhaps using the str.contains()
method as part of the merge?
Just for context my expected output would be something like this:
Group_Var Model
0RW52HC5K 0RW52HC5KDD13R
0RW52HC5K 0RW52HC5KDD13U
JJS42HC5J JJS42HC5JSSAYR
CodePudding user response:
Use pd.concat
with axis=1
:
df3 = pd.concat([df1, df2], axis=1)
Output:
>>> df3
Model Group_Var
0 0RW52HC5KDD13R 0RW52HC5K
1 0RW52HC5KDD13U 0RW52HC5K
2 JJS42HC5JSSAYR JJS42HC5J