Pandas Data Selection (loc / iloc / at)
Data selection is one of the most commonly used operations in Pandas. Understanding loc、iloc、at the differences and application scenarios lets you process data more efficiently.
The difference between loc and iloc
| Feature | loc | iloc |
|---|---|---|
| Indexing method | Label-based indexing (label-based) | Position-based indexing (integer-based) |
| Slicing | Includes the end position | Excludes the end position |
| Single value | Returns a scalar | Returns a scalar |
| Recommendation scenarios | When explicit index labels are available | When selecting by position |
Example
import
pandas
as
pd
Create a sample DataFrame
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
,
"Qian Qi"
]
,
"Age"
:
[
25
,
30
,
28
,
35
,
22
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Shenzhen"
,
"Hangzhou"
]
}
,
index
=
[
1
,
3
,
5
,
7
,
9
]
)
Note: the index is not contiguous
(
"DataFrame:"
)
(
df
)
(
)
loc: uses label-based indexing (includes the end position)
(
"df.loc[1:5] (label slicing, includes 5):"
)
(
df.
loc
[
1
:
5
]
)
(
)
iloc: uses position-based indexing (excludes the end position)
(
"df.iloc[0:2] (position slicing, excludes 2):"
)
(
df.
iloc
[
0
:
2
]
)
Usage of loc
Select rows
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
]
,
"Age"
:
[
25
,
30
,
28
,
35
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Shenzhen"
]
}
,
index
=
[
"a"
,
"b"
,
"c"
,
"d"
]
)
Select a single row (returns a Series)
(
"Select one row:"
)
(
df.
loc
[
"a"
]
)
(
)
Select multiple rows
(
"Select multiple rows:"
)
(
df.
loc
[
[
"a"
,
"c"
]
]
)
(
)
Slice selection (includes the start and end)
(
"Slice selection:"
)
(
df.
loc
[
"a"
:
"c"
]
)
Select columns
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
]
,
"Age"
:
[
25
,
30
,
28
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
]
}
,
index
=
[
"a"
,
"b"
,
"c"
]
)
Select a single column
(
"Select a single column:"
)
(
df.
loc
[
:
,
"Name"
]
)
(
)
Select multiple columns
(
"Select multiple columns:"
)
(
df.
loc
[
:
,
[
"Name"
,
"City"
]
]
)
(
)
Select columns by slicing
(
"Select columns by slicing:"
)
(
df.
loc
[
:
,
"Name"
:
"City"
]
)
Select specific rows and columns (recommended approach)
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
]
,
"Age"
:
[
25
,
30
,
28
,
35
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Shenzhen"
]
}
,
index
=
[
"a"
,
"b"
,
"c"
,
"d"
]
)
Select specific rows and columns
(
"Select a single value:"
)
(
df.
loc
[
"a"
,
"Name"
]
)
Returns "Zhang San"
(
type
(
df.
loc
[
"a"
,
"Name"
]
)
)
The type is str
(
)
Select multiple rows and multiple columns
(
"Select a subset:"
)
(
df.
loc
[
[
"a"
,
"c"
]
,
[
"Name"
,
"City"
]
]
)
(
)
Conditional selection
(
"Rows with age greater than 28:"
)
(
df.
loc
[
df
[
"Age"
]
28
]
)
Usage of iloc
Select by position
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
]
,
"Age"
:
[
25
,
30
,
28
,
35
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Shenzhen"
]
}
)
Select row 0
(
"Select row 0:"
)
(
df.
iloc
[
0
]
)
(
)
Select the first 3 rows
(
"Select the first 3 rows:"
)
(
df.
iloc
[
:
3
]
)
(
)
Select specific rows
(
"Select specific rows:"
)
(
df.
iloc
[
[
0
,
2
,
3
]
]
)
(
)
Negative indexing (starting from the end)
(
"Select the last row:"
)
(
df.
iloc
[
1
]
)
(
)
(
"Select the last 3 rows:"
)
(
df.
iloc
[
3
:
]
)
Select columns
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
]
,
"Age"
:
[
25
,
30
,
28
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
]
}
)
Select column 0
(
"Column 0:"
)
(
df.
iloc
[
:
,
0
]
)
(
)
Select columns 1 and 2
(
"Select multiple columns:"
)
(
df.
iloc
[
:
,
[
1
,
2
]
]
)
(
)
Select by slicing
(
"Select columns by slicing:"
)
(
df.
iloc
[
:
,
0
:
2
]
)
at and iat (getting a single value)
at and iat is an accessor dedicated to getting/setting a single value, and is loc and iloc>更快。
Example
import
pandas
as
pd
import
time
import
numpy
as
np
Create a large DataFrame for performance testing
df
=
pd.
DataFrame
(
np.
random
.
randn
(
1000
,
10
)
,
columns
=
[
f
"col_{i}"
for
i
in
range
(
10
)
]
)
at: get a single value (label-based indexing)
(
f
"at: {df.at[0, 'col_0']}"
)
iat: get a single value (position-based indexing)
(
f
"iat: {df.iat[0, 0]}"
)
Performance Comparison
n
=
10000
start
=
time
.
time
(
)
for
_
in
range
(
n
)
:
_
=
df.
iloc
[
0
,
0
]
(
f
"iloc time: {time.time() - start:.4f}s"
)
start
=
time
.
time
(
)
for
_
in
range
(
n
)
:
_
=
df.
iat
[
0
,
0
]
(
f
"iat time: {time.time() - start:.4f}s"
)
Conditional selection
Filtering data with boolean conditions is one of the most common operations.
Example
import
pandas
as
pd
df
=
pd.
DataFrame
(
{
"Name"
:
[
"Zhang San"
,
"Li Si"
,
"Wang Wu"
,
"Zhao Liu"
]
,
"Age"
:
[
25
,
30
,
28
,
35
]
,
"City"
:
[
"Beijing"
,
"Shanghai"
,
"Guangzhou"
,
"Beijing"
]
,
"Salary"
:
[
12000
,
15000
,
11000
,
18000
]
}
)
Single condition
(
"Employees older than 28:"
)
(
df
[
df
[
"Age"
]
28
]
)
(
)
Multiple conditions (using & | ~)
(
"In Beijing with salary greater than 12000:"
)
(
df
[
(
df
[
"City"
]
==
"Beijing"
)
&
(
df
[
"Salary"
]
12000
)
]
)
(
)
Filtering with isin
(
"City is Beijing or Shanghai:"
)
(
df
[
df
[
"City"
]
.
isin
(
[
"Beijing"
,
"Shanghai"
]
)
]
)
(
)
String containment
(
"Name contains 'three':"
)
(
df
[
df
[
"Name"
]
.
str
.
contains
(
Wed
)
]
)
Notes
1. Slicing includes the end position
loc slicing includes the end position,iloc does not include it.
2. A nonexistent index raises an error
Use loc raises a KeyError if the index does not exist; you can use loc[index_list] together with reindex。
3. Prefer using loc
Because loc is more readable and less error-prone, unless you need to select by position.
at/iatis the fastest way to access a single value,loc/ilocis used to select multiple values. Choosing the right method can improve code performance and readability.