Skip to main contentSkip to solution

You have two dataframes 'df1' and 'df2' having ID columns as common. To merge these two dataframes with only the common indices, we use (assuming pandas as pd is imported):

Solution

✅ Correct Option: 2

pd.merge() joins dataframes on a common column, here ID. Setting how = 'inner' keeps only the rows whose ID appears in both dataframes, which is exactly "only the common indices".


Option 3: how = 'outer' keeps all IDs from both dataframes, filling the missing values with NaN. It is a union, not an intersection.

Options 1 and 4: pd.concat() stacks dataframes along an axis. It does not accept how = 'outer'/'any' or on, and its first argument must be a list such as [df1, df2].

Merge types: inner gives the intersection, outer the union, left all rows of the left dataframe, right all rows of the right dataframe.

Keyboard Shortcuts

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