Index Alignment and Reindexing
Align two DataFrames to a common index with reindex(), fill missing labels, and understand how arithmetic aligns indices.
How Pandas Aligns Indices
One of Pandas' most powerful — and sometimes surprising — behaviours is automatic index alignment. When you add, subtract, or otherwise combine two Series or DataFrames, Pandas first aligns them on their index labels before performing the operation. Matching labels are operated on; non-matching labels produce NaN. This prevents silent errors from misaligned data and makes data from different sources safe to combine without manually sorting or reordering first.
import pandas as pd
s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s2 = pd.Series([1, 2, 3], index=['b', 'c', 'd'])
# Alignment in action: a→NaN, d→NaN
result = s1 + s2
print('s1 + s2 with automatic alignment:')
print(result)NaN from Misaligned Indices
When index labels don't match, Pandas fills the missing entries with NaN. This is intentional: it signals 'I had no corresponding value from one of the operands'. Always inspect the result of arithmetic between two Series or DataFrames for unexpected NaN values — they are a sign of index misalignment. Use fill_value=0 in the arithmetic method (s1.add(s2, fill_value=0)) to treat missing aligned values as zero instead of propagating NaN.
import pandas as pd
s1 = pd.Series({'a': 10, 'b': 20, 'c': 30})
s2 = pd.Series({'b': 1, 'c': 2, 'd': 3})
# Default: NaN where labels don't match
print('Default (NaN for unmatched):')
print(s1 + s2)
# fill_value=0: treat missing as zero
print('\nWith fill_value=0:')
print(s1.add(s2, fill_value=0))All lessons in this course
- Creating a MultiIndex
- Selecting Data from a MultiIndex
- Index Alignment and Reindexing
- Performance Benefits of Sorted Indices