← Back to Pandas Course | Chapter 2: Series | Lesson 2 of 7

Series indexing

Pick single values from a Series by label or by position.

In this page:

  1. Series indexing
Syntax
python
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

python
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
  1. Mixing up label-based and position-based access
  2. Requesting a missing label and getting KeyError
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.