Can we merge on more than one specific column?


#1

Question

In the context of this exercise, can we merge on more than one specific column?

Answer

Yes, you can perform a merge on one column, or on multiple specified columns, by passing in a list of the column names for each dataframe.

When listing multiple column names, it will only return rows for which all the column values match. Furthermore, the number of columns listed must match, and the order they are listed will matter.

Example

# This will match the values for 
# column "a" with "c" 
# and column "b" with "d".

pd.merge(
  df1,
  df2,
  left_on=["a", "b"],
  right_on=["c", "d"]
)