Explain Codes LogoExplain Codes Logo

How are iloc and loc different?

python
dataframe
iloc
loc
Alex KataevbyAlex Kataev·Dec 17, 2024
TLDR

Select by the integer index using iloc: df.iloc[0] fetches the first row. Choose by the label with loc: df.loc['index_label'] gets the row with the label 'index_label'. When slicing, iloc excludes the end point and loc includes it. Explore further to master these essential pandas tools!

In-depth explanation

Basic usage: Indexing

iloc uses integer-based indexing, similar to accessing lists, while loc uses label-based indexing, just like using keys in dictionaries.

# Accessing the first element print(df.iloc[0]) # Gotcha! # Accessing element by label print(df.loc['label']) # Tags are cool!

Slicing: The start, the end, and everything in between

When slicing, using loc includes the end point, while iloc does not.

# iloc excludes the end df.iloc[0:3] # We're on-board with Python slicing! # loc includes the end df.loc['start_label':'end_label'] # End-label party included!

Advanced slicing: Mixing label and position-based retrieval

The method get_loc() helps to get label positions, keeping iloc functionally high.

# iloc with get_loc df.iloc[0:df.columns.get_loc('column_label')] # Now you're speaking my language!

Sorting and indexing

iloc indexing is not influenced by DataFrame sorting. loc, however, adheres to the order!

# Sorting and indexing df.sort_values('column_name') df.iloc[1] # No idea who's in the sorting committee! df.loc['label'] # Oh hey, I know that guy!

Filters: Boolean vectors vs positional integers

loc supports Boolean vectors for filtering, while iloc loves integers.

# loc with Boolean vectors df.loc[df['column_name'] > 5] # Feeling a little picky today # iloc with integers df.iloc[[0,1,2]] # Positions, I choose you!

Non-integer indexes: String and DateTime

loc shines bright with non-integer indexes such as DateTime or strings.

# String indexing df.loc['my_string'] # String me along! # DateTime indexing df.loc['2023-01-01'] # Back to the future!

Accessing all dimensions

To select all rows or columns, both iloc and loc utilize colon :

# Getting all rows in the first column df.iloc[:, 0] # It's an all-you-can-index buffet!

Single column treasure

The good old indexing operator [] serves as a shortcut for single column retrieval.

# Single-column indexing df['my_column'] # Not all treasure is silver and gold, mate!

Consistent access vs order-sensitive retrieval

iloc gives consistent results based on positions, unaffected by DataFrame order unlike loc.

# Shuffle DataFrame df = df.sample(frac=1) # Consistent access df.iloc[0] # Same result, despite the dataframe dance shuffle! # Order-sensitive retrieval df.loc['label'] # Wait, where's my data?