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()
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’]
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()
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 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]
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.