Python
#11

loc vs iloc in Pandas: Label-Based vs Integer-Based Indexing

EasyFlipkart
Interview Question #11
Asked at Flipkart

Given a DataFrame with custom string index labels (e.g. ['c1', 'c2', 'c3']), demonstrate how to select rows 'c1' through 'c2' using `.loc`, and the first 2 rows using `.iloc`. Explain why .loc includes the endpoint whereas .iloc excludes it.

Input Table: df
3 rows preview
indexrevenueorders
c11200045
c21850072
c3940031
Expected Output Structure2 rows
indexrevenue
c112000
c218500
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

Python 3.10 (Pandas)
Environment Ready

Click "Run & Test" to execute your Pandas code against the test assertions.

Chat with us