Loc and iLoc Properties in Pandas Tutorial

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 primarily used for label indexing, while the .iloc[] property is mainly 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 | Feb 26, 2025

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 row or column label(s), or a boolean array. 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. It’s a data set that’s mainly used for beginners to help them create a model that predicts which passengers survived during the Titanic shipwreck.

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 provides us with a list of all male passengers. We can do the same process for every customer.

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. It’s also possible to use an integer index.

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 is integer position based, but it could also be used with a boolean array. 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. I can also extract a slice of the data set.

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, 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, while 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.