Which of the following statements is correct to merge two pandas DataFrames 'df1' and 'df2' based on common column 'ID'?
Which of the following statements is correct to merge two pandas DataFrames 'df1' and 'df2' based on common column 'ID'?
Solution
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.
Related questions:
2025: 30 May Shift 2