Can one perform a left join in pandas that selects only the first match on the right? Can one perform a left join in pandas that selects only the first match on the right? python python

Can one perform a left join in pandas that selects only the first match on the right?


Yes, you can use groupby to remove your duplicate lines. Do everything you've done to define left and right. Now, I define a new dataframe on your last line:

left2=left.merge( right, how='left', on='age' )df= left2.groupby(['age'])['salary'].first().reset_index()df

At first I used a .min(), which will give you the minimum salary at each age, as such:

df= left2.groupby(['age'])['salary'].min().reset_index()

But you were specifically asking about the first match. To do so you use the .first() option. Note: The .reset_index() at the end, just reformats the output of the groupby to be a dataframe again.