How to Use Loc and iLoc in Pandas: A Guide

The .loc[] and .iloc[] properties in Pandas are used to access specific rows and columns in a pandas DataFrame (or slice a data set). The .loc[] property is used for label indexing, while the .iloc[] property is used for integer indexing.

Written by Sohail Hosseini
A data cube sliced in pandas.
Image: Shutterstock / Built In
Brand Studio Logo
UPDATED BY
Brennan Whitfield | Aug 04, 2025
Summary: The .loc[] and .iloc[] properties in pandas are used to access rows and columns in a DataFrame. .loc[] selects data by labels or boolean arrays, while .iloc[] selects data by integer positions.

The .loc[] and .iloc[] properties in Pandas are used to access specific rows and columns in a pandas DataFrame. They can also be thought of as properties used to slice a pandas data set.

The .loc[] property accesses DataFrame rows and columns based on index or column labels, or a boolean array indexed by label. The .iloc[] property accesses DataFrame rows and columns based on integer location-based indexing or a boolean array.

What Do the Loc and iLoc Properties Do in Pandas?

The .loc[] and .iloc[] properties in Pandas are used to access specific rows and columns in a pandas DataFrame. The .loc property is typically used for label-based indexing and can access multiple columns, while the .iloc property is used for integer-based indexing.  

In order to apply these properties, I used the Titanic data set. This data set is commonly used in introductory machine learning tutorials.

First, let’s briefly look at the data set to see how many observations and columns it has.

titanic.head()
titanic.info()
Titanic machine learning data set.
Titanic machine learning data set. | Screenshot: Sohail Hosseini
Titanic data set in Pandas DataFrame.
Titanic data set in Pandas DataFrame. | Screenshot: Sohail Hosseini

More on Pandas: A Guide to Pandas Pivot Table

 

A tutorial on Pandas loc[] and iloc[] properties. | Video: CodeWithData

How to Use the Loc Property in Pandas

The .loc[] property is primarily label-based, but it’s also able to be used with a boolean array when we create statements.

If we want to look at only rows for male customers in the data set, we would use titanic.loc to locate with row and column names (also known as labels).

titanic.loc[titanic[‘Sex’]==’male’]
Pandas DataFrame filtered using the .loc function.
Pandas DataFrame filtered using the .loc property. | Screenshot: Sohail Hosseini

This returns a DataFrame containing all male passengers. You can apply similar filters to retrieve different passenger subsets.

pandas.DataFrame.loc can also be used for accessing multiple columns. For instance, if I want to locate all male passengers and ‘S’ (“Southampton”) for “Embarked,” I can create two conditions to give me a slice of the DataFrame.

titanic.loc[(titanic['Sex']=='male') & (titanic['Embarked']=='S')].head()
Titanic DataFrame filtered using the .loc function for sex and embarked status.
Titanic DataFrame filtered using the .loc property for sex and embarked status. | Screenshot: Sohail Hosseini

It’s important to use the .loc[] property with labels when we are using columns. As a note, the .loc[] property can use integer labels if the DataFrame’s index consists of integers (but it does not support positional indexing).

How to use .loc function with an integer index.
How to use .loc property with an integer index. | Screenshot: Sohail Hosseini
Integer sorting example with .loc function in Pandas.
Integer sorting example with .loc property in Pandas. | Screenshot: Sohail Hosseini

More on Pandas: A Beginner’s Guide to Using Pandas for Text Data Wrangling With Python

 

How to Use the iLoc Property in Pandas

The .iloc[] property selects data based on integer positions and can also accept a boolean array, provided the array aligns with row positions rather than index labels.

If we want to locate a cell of the data set, we can enter:

titanic.iloc[0,0]

This command gives the element at row = 0, and column = 0. You can also extract a subset of rows and columns using slice notation.

titanic.iloc[0:4,2:5]
Pandas Titanic DataFrame sorted using .iloc function.
Pandas Titanic DataFrame sorted using .iloc property. | Screenshot: Sohail Hosseini

In this case, it provides us rows 0 to 3 and columns 2 to 4.

The main purpose of using .loc[] and .iloc[] is to access rows and columns in a pandas DataFrame, based on label(s) or a boolean array for .loc[], or integer location-based indexing or boolean array for .iloc[]. The .loc[] property is primarily used for label indexing, and the .iloc[] property is mainly used for integer indexing. Try it yourself or follow along with the code used in this article.

Frequently Asked Questions

In the pandas library in Python, .loc[] is a property used to access a group of rows and columns in a pandas DataFrame by specified label(s) or a boolean array. The .loc[] property can take a single label, a list or array of labels or a slice object with labels as inputs. In pandas, a row or column name is known as a label.

In pandas in Python, the .iloc[] property accesses a group of DataFrame rows and columns by using integer location-based indexing (like df.iloc[0,0]) or boolean array.

The .loc[] property accesses DataFrame rows and columns by using their labels (like df.loc[‘row_1’, ‘column_1’]) or a boolean array.

In basic terms, .iloc[] is used for integer indexing and .loc[] is used for label indexing in a pandas DataFrame.

In the pandas library in Python, “loc” in .loc[] stands for “location,” and “iloc” in .iloc[] stands for “integer location.” This refers to the type of indexing each property uses to access DataFrame rows and columns.

Explore Job Matches.