Concept Flow - Merging on index
Start with two DataFrames
Identify index columns
Merge on index
Create merged DataFrame
Output
We start with two tables, identify their index columns, merge them using these indexes, and produce a combined table.
import pandas as pd df1 = pd.DataFrame({'A': [1, 2]}, index=['x', 'y']) df2 = pd.DataFrame({'B': [3, 4]}, index=['x', 'y']) merged = df1.merge(df2, left_index=True, right_index=True)
| Step | Action | df1 Index | df1 Columns | df2 Index | df2 Columns | Merge Condition | Merged Output |
|---|---|---|---|---|---|---|---|
| 1 | Create df1 | ['x', 'y'] | ['A'] | N/A | N/A | N/A | {'x': {'A':1}, 'y': {'A':2}} |
| 2 | Create df2 | N/A | N/A | ['x', 'y'] | ['B'] | N/A | {'x': {'B':3}, 'y': {'B':4}} |
| 3 | Merge on index | ['x', 'y'] | ['A'] | ['x', 'y'] | ['B'] | left_index=True, right_index=True | {'x': {'A':1, 'B':3}, 'y': {'A':2, 'B':4}} |
| 4 | Output merged DataFrame | ['x', 'y'] | ['A', 'B'] | ['x', 'y'] | ['B'] | Indexes matched | A B x 1 3 y 2 4 |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| df1 | undefined | {'x':1, 'y':2} with column A | {'x':1, 'y':2} with column A | {'x':1, 'y':2} with column A | {'x':1, 'y':2} with column A |
| df2 | undefined | undefined | {'x':3, 'y':4} with column B | {'x':3, 'y':4} with column B | {'x':3, 'y':4} with column B |
| merged | undefined | undefined | undefined | {'x': {'A':1, 'B':3}, 'y': {'A':2, 'B':4}} | {'x': {'A':1, 'B':3}, 'y': {'A':2, 'B':4}} |
Merging on index in pandas: Use df1.merge(df2, left_index=True, right_index=True) This joins DataFrames by their index labels. Only rows with matching indexes appear by default. Result combines columns side by side. Useful when index is meaningful key.