0Pricing
Data Science Academy · Lesson

Label-Based vs Position-Based Access

Choosing loc or iloc with confidence.

Label-Based vs Position-Based Access is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Ways to Reach In

A Series has both labels and positions, so there are two ways to grab a value: by its label or by its position number. Knowing which to use keeps your code clear. 🎯

Our Sample Series

Here is a small Series with string labels. We will use it to compare both access styles throughout this lesson.

import pandas as pd
s = pd.Series([90, 75, 88], index=['Ada', 'Bo', 'Cy'])

loc Uses Labels

The .loc accessor looks values up by their index label. You ask for a name and pandas returns that named value.

print(s.loc['Bo'])

iloc Uses Positions

The .iloc accessor works by integer position instead, counting from zero like a normal list. Position 1 is the second value.

print(s.iloc[1])

Same Value, Two Roads

With our Series, s.loc['Bo'] and s.iloc[1] return the same number. They reach it differently: one by name, one by spot.

print(s.loc['Bo'] == s.iloc[1])

Slicing With iloc

Position slices behave like list slices: the end is excluded. So iloc[0:2] returns the first two values only.

print(s.iloc[0:2])

Slicing With loc

Label slices are different: with .loc, the end label is included. Asking from 'Ada' to 'Bo' returns both endpoints.

print(s.loc['Ada':'Bo'])

The Inclusive Surprise

That inclusive end is the most common mix-up. Remember: iloc drops the last position, while loc keeps the last label. 🧠

Grab Several at Once

Pass a list of labels to .loc to pull many values together. The result is a smaller Series in the order you asked.

print(s.loc[['Ada', 'Cy']])

When Labels Are Numbers

If your index is integers, plain s[2] gets ambiguous. Using explicit .loc or .iloc removes all doubt about what you meant.

Pick With Confidence

Reach for .loc when you know the name, and .iloc when you know the spot. Being explicit makes your selections impossible to misread.

Quick Check

Test your grip on the two accessors.

Recap

You now choose .loc for labels and .iloc for positions, and you remember that loc slices include the end while iloc slices do not. Solid! 🎉

Frequently asked questions

Is the “Label-Based vs Position-Based Access” lesson free?

Yes — the full text of “Label-Based vs Position-Based Access” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.

What will I learn in “Label-Based vs Position-Based Access”?

Choosing loc or iloc with confidence. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Data Science Academy?

No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Label-Based vs Position-Based Access” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Data Science Academy lesson?

Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. A Column With a Name and Index
  2. Label-Based vs Position-Based Access
  3. Series Math and Alignment
  4. Counting and Spotting Values
← Back to Data Science Academy