What is the difference between .loc and .iloc in pandas?

Quick Overview

Asks for the difference between the pandas .loc and .iloc indexers. Tests whether you know how each one finds rows and columns, how slice endpoints behave, which selectors each accepts, and why the two can disagree on a DataFrame with an integer index.

What is the difference between .loc and .iloc in pandas?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is the difference between `.loc` and `.iloc` in pandas? ```hint Filter first, then index Build a small DataFrame with the default index, drop a few rows with a boolean filter, and compare what `.loc[2]` and `.iloc[2]` return on the result. ``` ### What a Strong Answer Covers - What each indexer uses to find rows and columns - How the end of a slice is handled by each, with a concrete slice - Which kinds of selectors each one accepts, including boolean filters - When the two return different rows for the same integer, and which everyday operations cause it ### Follow-up Questions - After filtering a DataFrame, how would you get back an index where labels and positions agree again? - How would you set a value in every row that matches a condition, and why is one `.loc` call preferred over chained indexing? - Can `.iloc` take a boolean mask, and if so, what form must the mask have? - On a sorted `DatetimeIndex`, what does `df.loc["2024-01":"2024-03"]` return?

Overview: Asks for the difference between the pandas .loc and .iloc indexers. Tests whether you know how each one finds rows and columns, how slice endpoints behave, which selectors each accepts, and why the two can disagree on a DataFrame with an integer index.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What is the difference between .loc and .iloc in pandas?

What a Strong Answer Covers Guidance

  • What each indexer uses to find rows and columns
  • How the end of a slice is handled by each, with a concrete slice
  • Which kinds of selectors each one accepts, including boolean filters
  • When the two return different rows for the same integer, and which everyday operations cause it

Follow-up Questions Guidance

  • After filtering a DataFrame, how would you get back an index where labels and positions agree again?
  • How would you set a value in every row that matches a condition, and why is one .loc call preferred over chained indexing?
  • Can .iloc take a boolean mask, and if so, what form must the mask have?
  • On a sorted DatetimeIndex , what does df.loc["2024-01":"2024-03"] return?