---
source: https://iinventedlight.com/library/notes/pandas-indexing-loc-iloc.md
title: "pandas selection with loc and iloc"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_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); prose restates or closely paraphrases cited source lines"
license_family: pandas-bsd3
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: pandas"
tags: ['pandas', 'indexing', 'loc', 'iloc']
sources:
  - id: S1
    title: "Indexing and selecting data — pandas 3.1.1 documentation"
    publisher: pandas docs
    url: https://pandas.pydata.org/docs/user_guide/indexing.html
    accessed: 2026-11-04
    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)"
  - id: S2
    title: "How do I select a subset of a DataFrame? — pandas 3.1.1 documentation"
    publisher: pandas docs
    url: https://pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html
    accessed: 2026-11-04
    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)"
---
# 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).
