BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Left-on and right-on parameter in excersice not clear

Screen Link:

My Code:

merged = pd.merge(left=happiness2015, right=world_dev, how='left', left_on='Country', right_on='ShortName')

These exercises do not explain what the left-on and right-on parameters do. So my question is:
What do they modify and when do you use it?

1 Like

Hi @jeroenstikkelorum,

left_on is the name of the column (or of the index level, but in our case it’s a column) in the left dataframe on which we join, right_on - in the right one. In other words, when merging, Python is taking each value from the column assigned as left_on in the left dataframe and is looking for the same value in the column assigned as right_on in the right dataframe. And if it finds the same value in the right dataframes, it merges these rows.

In this case we have also how='left'. Hence, if Python doesn’t find a corresponding value in the right dataframe, it preserves the row with that value from the left dataframe anyway, and adds all the missing values as NaN.

2 Likes

thanks for your clear explanation

Am i right that you use left_on and right_on when the column names are not the same?

earlier in the course, i had to use the parameter on= in stead of left_on and right_on

merged = pd.merge(left=three_2015, right=three_2016, on='Country')

1 Like

Yes, absolutely. In case the column names are the same, you just use on, instead of left_on and right_on.

2 Likes