Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Data Selection (loc / iloc / at)

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

print

(

"DataFrame:"

)

print

(

df

)

print

(

)

loc: uses label-based indexing (includes the end position)

print

(

"df.loc[1:5] (label slicing, includes 5):"

)

print

(

df.

loc

[

1

:

5

]

)

print

(

)

iloc: uses position-based indexing (excludes the end position)

print

(

"df.iloc[0:2] (position slicing, excludes 2):"

)

print

(

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)

print

(

"Select one row:"

)

print

(

df.

loc

[

"a"

]

)

print

(

)

Select multiple rows

print

(

"Select multiple rows:"

)

print

(

df.

loc

[

[

"a"

,

"c"

]

]

)

print

(

)

Slice selection (includes the start and end)

print

(

"Slice selection:"

)

print

(

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

print

(

"Select a single column:"

)

print

(

df.

loc

[

:

,

"Name"

]

)

print

(

)

Select multiple columns

print

(

"Select multiple columns:"

)

print

(

df.

loc

[

:

,

[

"Name"

,

"City"

]

]

)

print

(

)

Select columns by slicing

print

(

"Select columns by slicing:"

)

print

(

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

print

(

"Select a single value:"

)

print

(

df.

loc

[

"a"

,

"Name"

]

)

 

Returns "Zhang San"

print

(

type

(

df.

loc

[

"a"

,

"Name"

]

)

)

 

The type is str

print

(

)

Select multiple rows and multiple columns

print

(

"Select a subset:"

)

print

(

df.

loc

[

[

"a"

,

"c"

]

,

[

"Name"

,

"City"

]

]

)

print

(

)

Conditional selection

print

(

"Rows with age greater than 28:"

)

print

(

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

print

(

"Select row 0:"

)

print

(

df.

iloc

[

0

]

)

print

(

)

Select the first 3 rows

print

(

"Select the first 3 rows:"

)

print

(

df.

iloc

[

:

3

]

)

print

(

)

Select specific rows

print

(

"Select specific rows:"

)

print

(

df.

iloc

[

[

0

,

2

,

3

]

]

)

print

(

)

Negative indexing (starting from the end)

print

(

"Select the last row:"

)

print

(

df.

iloc

[

1

]

)

print

(

)

print

(

"Select the last 3 rows:"

)

print

(

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

print

(

"Column 0:"

)

print

(

df.

iloc

[

:

,

0

]

)

print

(

)

Select columns 1 and 2

print

(

"Select multiple columns:"

)

print

(

df.

iloc

[

:

,

[

1

,

2

]

]

)

print

(

)

Select by slicing

print

(

"Select columns by slicing:"

)

print

(

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)

print

(

f

"at: {df.at[0, 'col_0']}"

)

iat: get a single value (position-based indexing)

print

(

f

"iat: {df.iat[0, 0]}"

)

Performance Comparison

n

=

10000

start

=

time

.

time

(

)

for

_

in

range

(

n

)

:

    _

=

df.

iloc

[

0

,

0

]

print

(

f

"iloc time: {time.time() - start:.4f}s"

)

start

=

time

.

time

(

)

for

_

in

range

(

n

)

:

    _

=

df.

iat

[

0

,

0

]

print

(

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

print

(

"Employees older than 28:"

)

print

(

df

[

df

[

"Age"

]

28

]

)

print

(

)

Multiple conditions (using & | ~)

print

(

"In Beijing with salary greater than 12000:"

)

print

(

df

[

(

df

[

"City"

]

==

"Beijing"

)

&

(

df

[

"Salary"

]

12000

)

]

)

print

(

)

Filtering with isin

print

(

"City is Beijing or Shanghai:"

)

print

(

df

[

df

[

"City"

]

.

isin

(

[

"Beijing"

,

"Shanghai"

]

)

]

)

print

(

)

String containment

print

(

"Name contains 'three':"

)

print

(

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/iat is the fastest way to access a single value,loc/iloc is used to select multiple values. Choosing the right method can improve code performance and readability.