In the Python pandas library, DataFrame.loc[] is a property that lets you select data from a DataFrame using labels. This makes it easy to extract specific rows and columns from a DataFrame.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

What is the syntax for pandas loc[]?

The syntax for loc[] is quite simple. All you need to do is pass the labels of the columns and rows you want to select as a parameter:

DataFrame.loc[selection]
python

With pandas loc[], se­lec­tions are primarily made using labels. This means the parameter you provide can be a single label, a list or a slice of labels. Boolean arrays can also be used as well.

What is the dif­fer­ence between loc[] and iloc[]?

While pandas DataFrame.loc[] selects data based on labels, DataFrame.iloc selects data based on integer-based positions. Here’s a code example to help il­lus­trate the dif­fer­ences. First, we’re going to create a pandas DataFrame:

import pandas as pd
# Example DataFrame
data = {'Name': ['Alyssa', 'Brandon', 'Carmen'], 'Age': [23, 35, 30]}
df = pd.DataFrame(data)
print(df)
python

Here’s what the DataFrame looks like:

Name    Age
0   Alyssa     	23
1 	Brandon     35
2  	Carmen     	30

To extract ‘Alyssa’ from the DataFrame, you can use both pandas loc[] and iloc[]. Although the approach differs, the result is the same:

# Using loc and labels to extract Alyssa
print(df.loc[0, 'Name'])  # Output: 'Alyssa'
# Using iloc and integers to extract Alysa
print(df.iloc[0, 0])  # Output: 'Alyssa'
python

How to use pandas DataFrame.loc[]

Pandas loc[] helps you extract subsets of your DataFrame. With loc[], you can extract a single row or column, multiple rows and columns or even apply con­di­tions for filtering. This flex­ib­il­ity makes it suitable for a variety of use cases.

Selecting a single row

Let’s look at a DataFrame example:

import pandas as pd
data = {
    'Name': ['Alyssa', 'Brandon', 'Carmen'],
    'Age': [23, 35, 30],
    'City': ['Sheffield', 'Glasgow', 'Belfast']
}
df = pd.DataFrame(data)
print(df)
python

Here’s what the resulting DataFrame looks like:

Name  	Age      City
0   Alyssa  23	 	Sheffield
1 Brandon  	35    Glasgow
2 Carmen    30    Belfast

To select the data from the row that contains in­form­a­tion about Brandon (index 1), you can use pandas loc[]:

brandon_data = df.loc[1]
print(brandon_data)
python

Here’s the result:

Name         Brandon
Age              35
City        	Glasgow
Name: 1, dtype: object

Selecting multiple columns

You can also use DataFrame.loc[] to select a subset of columns. The following code selects the columns ‘Name’ and ‘City’:

name_city = df.loc[:, ['Name', 'City']]
print(name_city)
python

The result is a subset of the original DataFrame:

Name     City
0   Alyssa  Sheffield
1 Brandon   Glasgow
2  Carmen   Belfast

Selecting rows based on con­di­tions

With pandas loc[], you can also select rows that meet specific criteria. You can do this with Boolean com­par­is­on operators. For example, here’s how to filter out all in­di­vidu­als who are older than 25:

older_than_25 = df.loc[df['Age'] > 25]
print(older_than_25)
python

The code above produces a DataFrame that only includes data for in­di­vidu­als in the DataFrame who are older than 25:

Name  	Age     City
1 Brandon     35   Glasgow
2  Carmen     30   Belfast
Go to Main Menu