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


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

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

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