pandas selection with loc and iloc
Answer: pandas supports three types of multi-axis indexing. .loc is primarily label based, but may also be used with a boolean array, and .loc will raise KeyError when the items are not found. (S1)
Rows, then columns
- When using loc/iloc, the part before the comma is the rows you want, and the part after the comma is the columns you want to select. (S2)
- For both parts, you can use a single label, a list of labels, a slice of labels, a conditional expression or a colon. (S2)
- When selecting specific rows and/or columns with loc or iloc, new values can be assigned to the selected data. (S2)
Label versus position
- In .loc, a single label such as 5 is interpreted as a label of the index. (S1)
- The most robust and consistent way of slicing ranges along arbitrary axes is the .iloc method, described in the Selection by Position section. (S1)
- .loc, .iloc, and also [] indexing can accept a callable as indexer. (S1)
- pandas aligns all axes when setting Series and DataFrame from .loc. (S1)
- However, pandas does not align axes when setting from .iloc because .iloc operates by position. (S1)
Claims
- id: C1 claim: .loc is primarily label based. source: S1 evidence: ".loc is primarily label based, but may also be used with a boolean array."
- id: C2 claim: .loc raises KeyError for missing items. source: S1 evidence: ".loc will raise KeyError when the items are not found."
- id: C3 claim: Before the comma is rows; after is columns. source: S2 evidence: "is the rows you want, and the part after the comma is the columns you"
- id: C4 claim: .iloc operates by position. source: S1 evidence: "because .iloc operates by position."
Sources
- [S1] Indexing and selecting data — pandas 3.1.1 documentation. pandas docs. https://pandas.pydata.org/docs/user_guide/indexing.html . License: BSD-3-Clause (pandas-dev/pandas LICENSE; Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team; Copyright (c) 2011-2026, Open source contributors; https://github.com/pandas-dev/pandas/blob/main/LICENSE). Accessed 2026-11-04.
- [S2] How do I select a subset of a DataFrame? — pandas 3.1.1 documentation. pandas docs. https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html . License: BSD-3-Clause (pandas-dev/pandas LICENSE; Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team; Copyright (c) 2011-2026, Open source contributors; https://github.com/pandas-dev/pandas/blob/main/LICENSE). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pandas docs, Indexing and selecting data — pandas 3.1.1 documentation, BSD-3-Clause (pandas-dev/pandas LICENSE; Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team; Copyright (c) 2011-2026, Open source contributors; https://github.com/pandas-dev/pandas/blob/main/LICENSE); pandas docs, How do I select a subset of a DataFrame? — pandas 3.1.1 documentation, BSD-3-Clause (pandas-dev/pandas LICENSE; Copyright (c) 2008-2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team; Copyright (c) 2011-2026, Open source contributors; https://github.com/pandas-dev/pandas/blob/main/LICENSE).
Cite this
iinventedlight. 2026. "pandas selection with loc and iloc." iinventedlight Library, iil:note:ha0s3t0k, version of 2026-11-04. https://iinventedlight.com/library/notes/pandas-indexing-loc-iloc/. License: CC-BY-4.0.
@misc{iil_note_ha0s3t0k,
author = {{iinventedlight}},
title = {{pandas selection with loc and iloc}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/pandas-indexing-loc-iloc/},
note = {Permanent ID iil:note:ha0s3t0k. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/pandas-indexing-loc-iloc.md.