The loc and iloc functions in Pandas are used to slice a data set. In order to apply these functions, 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. 

What Do the Functions Loc and iLoc Do in Pandas?

Loc and iloc are two functions in Pandas that are used to slice a data set in a Pandas DataFrame. The function .loc is typically used for label indexing and can access multiple columns, while .iloc is used for integer indexing.   

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

 

How to Use the Loc Function in Pandas

This function is primarily label based, but it’s also used with a boolean array when we create statements. If we want to look at only rows for male customers here, we would use pandas. DataFrame.loc to locate with labels.

titanic.loc[titanic[‘Sex’]==’male’]
Pandas DataFrame filtered using the .loc function.
Pandas DataFrame filtered using the .loc function. | 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 is 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 function for sex and embarked status. | Screenshot: Sohail Hosseini

It’s important to use the .loc function 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 function with an integer index. | Screenshot: Sohail Hosseini
Integer sorting example with .loc function in Pandas.
Integer sorting example with .loc function in Pandas. | Screenshot: Sohail Hosseini
A tutorial on Pandas loc and iloc functions. | Video: CodeWithData

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

 

How to Use the iLoc Function

The .iloc function 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 function. | Screenshot: Sohail Hosseini

In this case, it provides us rows zero-to-three and columns two-to-four.

The main purpose of using .loc and .iloc is to slice the DataFrame in Pandas. Function .loc is primarily used for label indexing, and the .iloc function is mainly used for integer indexing. Try it yourself or follow along with the code used in this article.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us