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):
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
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.
Related questions:
2026: 19 May Shift 1
2026: 29 May Shift 1
2025: 14 May Shift 1