What is the difference between loc and iloc?
PICTURE THIS: DATABASE INDEX
Simple meaning
loc selects by label, including index values and column names, while iloc selects by integer position.
WHY — Pandas instead of guessing?
Why interviewers care about Pandas:
contrast on Pandas, not two memorised paragraphs.
the developer, then one case where picking wrong hurts.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1loc selects by label,
including index values and column names, while iloc selects by integer position.
- 2Why it exists
loc[:, 'revenue'] is name-based
- 3iloc[:, 3] is the
fourth column regardless of name.
- 4Mixing them up is
a common source of off-by-one bugs after a sort or reset_index.
- 5Common mistake
What juniors usually get wrong.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
loc selects by label, including index values and column names, while iloc selects by integer position. loc[:, 'revenue'] is name-based