Skip to main contentSkip to solution

Which of the following statements is correct to merge two pandas DataFrames 'df1' and 'df2' based on common column 'ID'?

Solution

✅ Correct Option: 2

df1.merge(df2, on='ID') is the DataFrame method for a database-style join on a column that exists in both DataFrames. It matches rows where the 'ID' values are equal and returns an inner join by default.


Option 4: join matches the calling DataFrame's column 'ID' against the index of df2, not against a column called 'ID' in df2. It works as a column-to-column merge only if df2 has 'ID' set as its index.

Option 3: concat is a top-level function, called as pd.concat([df1, df2]). It is not a DataFrame method and has no on parameter.

Option 1: df1.df2 is not valid merge syntax. It tries to access an attribute named df2 on df1.

Keyboard Shortcuts

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