Python
All Top 50 Python & Pandas Interview Questions#11
loc vs iloc in Pandas: Label-Based vs Integer-Based Indexing
EasyFlipkartInterview Question #11
Asked at FlipkartGiven 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| index | revenue | orders |
|---|---|---|
| c1 | 12000 | 45 |
| c2 | 18500 | 72 |
| c3 | 9400 | 31 |
Expected Output Structure2 rows
| index | revenue |
|---|---|
| c1 | 12000 |
| c2 | 18500 |
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.