Series indexing
Pick single values from a Series by label or by position.
In this page:
Syntax
s["label"]
s.loc["label"]
s.iloc[position]
Series indexing
Use square brackets with a label, s["b"], or .loc for labels and .iloc for integer positions. Being explicit with .loc and .iloc avoids confusion when the labels are themselves integers. A list of labels returns a smaller Series.
Note:
With integer labels, s[0] may mean a label, not a position; use iloc to be safe.
Example: Series indexing
import pandas as pd
s = pd.Series([10, 20, 30], index=["a", "b", "c"])
print(s["b"])
print(s.loc["c"], s.iloc[0])
print(s[["a", "c"]])
# Output:
# 20
# 30 10
# a 10
# c 30
# dtype: int64
Related Topics
Common Mistakes
- Mixing up label-based and position-based access
- Requesting a missing label and getting KeyError
- Using s[0] on an integer-labelled index
Chapter Summary
- Brackets and loc use labels
- iloc uses positions
- A list of labels returns a Series
- Missing labels raise KeyError
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: