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()


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’]

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()

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 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]

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
What is loc() in pandas?
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.
How are Iloc() and loc() different?
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.
What does the LOC stand for in Python?
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.