Skip to main contentSkip to solution

You want to create a new DataFrame 'df3' by merging two DataFrames 'df1', 'df2' on the 'Roll_no' column, then which of the following is the correct syntax for this?

Solution

✅ Correct Option: 1

pd.merge(df1, df2, on='Roll_no') joins the two DataFrames on the common column 'Roll_no'. By default it does an inner join, keeping only the rows whose Roll_no appears in both.


Option 2: join() matches the calling DataFrame's 'Roll_no' column against the index of df2, not against a 'Roll_no' column in df2, so it does not merge on the column in both.

Option 3: pd.concat is a function and needs round brackets, but here it is written with square brackets and mismatched brackets. Also, concat stacks or aligns DataFrames by index and does not merge on a column.

Option 4: pd.add does not exist for combining DataFrames, and the brackets are mismatched.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question