Selecting and Transforming Series
Select without ambiguity
s.loc["Ada"] # one label
s.loc[["Ada", "Sam"]] # several labels
s.iloc[0] # first position
s.iloc[:3] # first three positions
s[s.ge(80)] # boolean filter
Use .loc for labels and .iloc for positions, especially when the index itself
contains integers. Bare s[key] is concise for an unambiguous label, but explicit
indexers communicate intent and survive index changes better.
Transform in this order
- Use native vectorized arithmetic or string/datetime accessors.
- Use
mapfor a scalar lookup or elementwise function. - Use
where,mask,replace, orfillnafor conditional replacement. - Use
applyonly when no clearer vectorized operation exists.
normalized = (s - s.mean()) / s.std()
bands = s.map({90: "A", 80: "B"})
clipped = s.clip(lower=0)
A dictionary passed to map turns unmatched values into missing values. Use
replace when unmatched values should remain unchanged.
Combine Series
Use pd.concat to stack or place objects side by side:
stacked = pd.concat([train, test], ignore_index=True)
table = pd.concat([actual.rename("actual"), predicted.rename("predicted")], axis=1)
Series.append is not the combination API. Before column-wise concatenation,
check whether label alignment is intended.
Iteration
If iteration is genuinely necessary, s.items() yields (label, value) pairs.
Do not use iteration for arithmetic or routine filtering; vectorized expressions
are clearer and usually faster.