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?
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
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.
Related questions:
2026: 26 May Shift 1
2025: 3 June Shift 2
2025: 27 May Shift 2