pandas / Basic: labels, types and accepted input / Lesson 2
Selection, loc, iloc and alignment
Choose labels or positions deliberately and predict how labeled operands align before arithmetic or assignment.
On this page
Lesson goals
- Distinguish loc label selection from iloc positional selection.
- Explain automatic label alignment and index-dependent results.
An integer label is not necessarily a position
loc interprets labels, while iloc interprets integer positions. With index labels ten, twenty and thirty, loc[20] selects the row labeled twenty and iloc[1] selects the second row. They happen to identify the same row in that fixture but express different contracts. Label slices generally include both present endpoints under documented conditions, while positional slices follow Python’s excluded-stop convention. Missing labels and out-of-range scalar positions also raise different errors. Keep index sorting and duplicate-label assumptions explicit for slicing; do not generalize a convenient result from one monotonic unique index to every possible index structure.
Compare two explicit slices
The unexecuted pandas example uses a unique increasing integer index. loc[10:20] includes labels ten and twenty, while iloc[0:2] includes positions zero and one. Both results contain two rows because their boundaries were chosen to match, not because the APIs are interchangeable. Reordering the frame changes positional meaning while label meaning remains attached to the labels. Use explicit row and column selectors when teaching or reviewing a transformation. A bare bracket expression can select a column, mask or other behavior depending on the object, so its intent should not rely on the reader guessing from a single integer.
import pandas as pd
frame = pd.DataFrame({'units': [2, 3, 4]}, index=[10, 20, 30])
assert frame.loc[10:20].index.tolist() == [10, 20]
assert frame.iloc[0:2].index.tolist() == [10, 20]
print(frame.loc[10:20].index.tolist())
[10, 20]
Expected output is[10,20] under the unique increasing index fixture; no pandas execution occurred.
Arithmetic matches labels rather than visual row order
When labeled Series participate in many operations, pandas aligns their indexes. A right-hand series ordered B then A does not simply add its first value to the left’s first visible row A. Matching labels can prevent a positional mistake, but unexpected labels can introduce missing results or a larger aligned index. Assignment of a Series to a column also uses labels under its documented contract. Converting to an unlabeled array deliberately changes that relationship and is safe only when positional correspondence has been established. Check the expected key set and order instead of stripping labels merely to silence an alignment surprise.
Model the intended key match independently
The standard-library dictionaries calculate A=12 and B=23 using key lookup even though the right dictionary was declared in the opposite order. It models a chosen business-key match only, not pandas duplicate-index handling, missing-value propagation or broadcasting. The expected result can be used to review the labeled expression independently of display formatting. Add a missing-key fixture before claiming a complete input contract. In a real pipeline, a new unexpected identifier may require rejection or reconciliation rather than silently accepting an outer alignment that changes the population used in a statistic.
left = {'A': 10, 'B': 20}
right = {'B': 3, 'A': 2}
matched = {key: value + right[key] for key, value in left.items()}
assert matched == {'A': 12, 'B': 23}
print(matched)
{'A': 12, 'B': 23}
Dictionary lookup expresses the expected business match and does not emulate pandas index union or missingness.
Check the selected population before calculating
A selection review should state expected labels, output shape and whether duplicates or missing mask values are allowed. A Boolean Series mask is itself labeled and can align differently from an unlabeled Boolean array; use the selected API’s documented rules. Under the 3.1 baseline, Copy-on-Write means a selected object behaves independently for mutation, so selection is not a promise that later edits update the original. This lesson executes no pandas selection or alignment. Its dictionary model is separately labeled, and the real snippets remain illustrative with derived expected results rather than fabricated engine observations.
Common errors and fixes
loc[1] unexpectedly raises KeyError on an index starting at10.
- Why it happens
- The integer was intended as a position but interpreted as a label.
- How to fix it
- Use iloc for the intended position or select an existing label explicitly.
- Prevent it next time
- State label-versus-position intent in every selection contract.
A reordered input attaches values to the wrong entities after labels are removed.
- Why it happens
- Positional correspondence was assumed without validation.
- How to fix it
- Preserve keys and align deliberately before any positional conversion.
- Prevent it next time
- Test shuffled input order against expected keyed results.
Unexpected missing values appear after arithmetic.
- Why it happens
- Operand label sets differed and alignment introduced unmatched positions.
- How to fix it
- Inspect key coverage and choose an explicit missing-key policy.
- Prevent it next time
- Validate index identity and uniqueness before combining operands.
Check your understanding
1. If the frame is reordered, must iloc[0] still identify item A?
Show worked solution
No. iloc describes the first current position. Label-based identity remains attached to its label only when the index contract is preserved. Use explicit business keys when identity matters, and check the selected population after sorting rather than relying on a remembered display order.
2. Why can removing labels to obtain a convenient numeric result be analytically unsafe?
Show worked solution
It can replace key matching with positional pairing and conceal missing or unexpected entities. A result with no missing values can still join the wrong measurements. First establish key coverage, duplicates and intended ordering, then make any unlabeled conversion as an explicit documented decision.
Key takeaways
- Labels and positions are different selection contracts.
- Alignment affects the population and meaning of a calculation.