Home >Backend Development >Python Tutorial >What's the Difference Between Pandas `loc` and `iloc` for Data Selection?

What's the Difference Between Pandas `loc` and `iloc` for Data Selection?

Linda Hamilton
Linda HamiltonOriginal
2024-12-20 03:10:10155browse

What's the Difference Between Pandas `loc` and `iloc` for Data Selection?

How iloc and loc Differ in Pandas:

In data manipulation using Pandas, iloc and loc are two commonly used slicing methods that often cause confusion. Understanding their fundamental difference is essential for efficient data management.

Label vs. Location

The key distinction between loc and iloc lies in how they select data:

  • loc: Accesses data based on labels, such as row or column names (index and column labels).
  • iloc: Accesses data based on integer locations within the object's axis (row/column numbers).

Examples:

Consider a DataFrame df with a non-monotonic index containing letters:

import pandas as pd

df = pd.DataFrame({'col1': ['a', 'b', 'c', 'd', 'e', 'f']}, index=[49, 48, 47, 0, 1, 2])

loc (Label-Based Slicing):

  • df.loc[0]: Retrieves the row with index label "0" ('d').
  • df.loc[0:1]: Returns two rows with labels 0 and 1 ('d' and 'e').

iloc (Location-Based Slicing):

  • df.iloc[0]: Gets the value at index location 0 ('a').
  • df.iloc[0:1]: Selects one row at index location 0 ('a').

Key Differences:

The following table highlights the differences between loc and iloc in various scenarios:

Object Description loc iloc
0 Single item Value at index label 0 ('d') Value at index location 0 ('a')
0:1 Slice Two rows (labels 0 and 1) One row (first row at location 0)
1:47 Slice with out-of-bounds end Zero rows (empty Series) Five rows (location 1 onwards)
1:47:-1 Slice with negative step Three rows (labels 1 back to 47) Zero rows (empty Series)
[2, 0] Integer list Two rows with given labels Two rows with given locations
s > 'e' Boolean series (indicating true values) One row (containing 'f') NotImplementedError
(s>'e').values Boolean array One row (containing 'f') Same as loc
999 Integer object not in index KeyError IndexError (out of bounds)
-1 Integer object not in index KeyError Returns last value in s
lambda x: x.index[3] Callable applied to series (here returning 3rd item in index) s.loc[s.index[3]] s.iloc[s.index[3]]

The above is the detailed content of What's the Difference Between Pandas `loc` and `iloc` for Data Selection?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn