LambdaLab
Informatics Practices · Class 12 · Python Pandas · Series
PandasSeries⏱️ 11 min read

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.
Note
You control the labels, but Pandas controls the positions— they're created automatically, always start at 0, and can't be changed. (If you never set custom labels, the labels and positions happen to look the same: 0, 1, 2, …)

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 -1 for the last item.
For scores = [95, 88, 72, 91, 84, 99]
LabelValuePositiveNegative
'a'950-6
'b'881-5
'c'722-4
'd'913-3
'e'844-2
'f'995-1
Note
Remember: you choose the labels, but Pandas assigns these integer positions by default. They always start at 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.
selection.py
Tip
.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:

Key Takeaway
.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.
loc vs iloc slicer

The one that trips everyone up. Slice by label (.loc, end INCLUSIVE) or by position (.iloc, end EXCLUSIVE) and watch which items survive.

0/-61/-52/-43/-34/-25/-1
label · value
a95
b88
c72
d91
e84
f99
you wrote
s.loc['a':'e']
result
a95
b88
c72
d91
e84
END LABEL is included ✓

5Slicing with .loc (inclusive)

slice_loc.py

6Slicing with .iloc (exclusive)

slice_iloc.py

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.

slice_step.py
Quick Check

scores has labels a–f. What does scores.loc['b':'d'] return?

Quick Check

What does scores.iloc[1:4] return?

Quick Check

How do you reverse a Series s?