Reputation: 145
I am trying to perform a vlookup between two dataframes (df1, df2), the column present in both dfs is 'string'. The problem is that in df1, where I should bring the values, the 'string'column has duplicates and I need to have them there so, don't want or need to remove them but for all of them I have to bring the value from df2.
My dfs looks like this
df1 df2 expected result(keep df1)
string col2 `string random string col2
A A something A something
A B something A something
A C something A something
B B something
B B something
B B something
C C something
C C something
C C something
Is there a way of doing this? many thanks in advance
Upvotes: 1
Views: 376
Reputation: 41
df1 = pd.DataFrame([
['A', '-'],['A', '-'],['A', '-'],
['B', '-'],['B', '-'],['B', '-'],
['C', '-'],['C', '-'],['C', '-'],
], columns=['string', 'col2'])
df2 = pd.DataFrame([
['A', 'something'],
['B', 'something1'],
['C', 'something3'],
], columns=['string', 'random'])
print(df1, '\n\n',df2)
result_df = df1.set_index('string').join(df2.set_index('string'))
Upvotes: 1