Selection, Indexing & Slicing
This is the topic that trips everyone up — so we'll go slowly. Every item in a Series can be reached two ways, and getting them straight makes selecting and slicing effortless.
1Two ways to find data: label vs position
Every item in a Series has two identifiers:
- 🏷️ Index label — the custom name you gave the row (e.g.
'a','b'). - 🔢 Integer position— the row's place in the sequence, always starting from 0, no matter what the labels are.
2Positive and negative positions
Before we start selecting, know that every position can be written two ways — this matters for both selecting and slicing:
- ➡️ Positive positions: count from the start, from
0. - ⬅️ Negative positions: count from the end, from
-1for the last item.
| Label | Value | Positive | Negative |
|---|---|---|---|
| 'a' | 95 | 0 | -6 |
| 'b' | 88 | 1 | -5 |
| 'c' | 72 | 2 | -4 |
| 'd' | 91 | 3 | -3 |
| 'e' | 84 | 4 | -2 |
| 'f' | 99 | 5 | -1 |
0, and you have no control over them — that's also what the animated card on the right is showing.3Selecting one item: .loc, .iloc, [ ] and .at
There are a few ways to grab a single item. .loc and .iloc are the clearest — they say outright whether you mean a label or a position:
- 🎈
.loc[label]— select by the custom label. - 🔢
.iloc[position]— select by the integer position. - ⚡
[ ]— a shortcut that uses the label. - 🎯
.at[label]— also uses the label, but only ever returns a single value. It's the fastest way to fetch one item.
.at is like .loc for a single cell — it can only ever return a single value, never a slice, which is what makes it the quickest. Its position-based twin is .iat[position].4Slicing a Series — the interactive way
Slicing grabs a range of items. There's one golden rule that catches everyone:
.loc (labels) is inclusive — the end label is kept. .iloc (positions) is exclusive — the end position is left out. Play with both below until it clicks.The one that trips everyone up. Slice by label (.loc, end INCLUSIVE) or by position (.iloc, end EXCLUSIVE) and watch which items survive.
5Slicing with .loc (inclusive)
6Slicing with .iloc (exclusive)
7Adding a step (the "jump")
A third number sets a step — how far to jump each time. The syntax is start : end : step. A negative step walks backwards, which is the classic way to reverse a Series.
scores has labels a–f. What does scores.loc['b':'d'] return?
What does scores.iloc[1:4] return?
How do you reverse a Series s?