pandas basics: loc vs. iloc

Einblick Content Team - April 7th, 2023

In pandas, there are several ways to subset your data to get specific rows, columns, or values. Two that can be easily confused are df.loc[] and df.iloc[]. Note that these are properties, not functions, and therefore use square brackets rather than parentheses. The main difference is as follows:

  • df.loc[] uses row and column labels to index the values. (i.e. row “movie_1”, “movie_2”, or column “title”, “director”, “rating”)
  • df.iloc[] uses row and column integer-based positions to index the values. (i.e. row 0, 1, 2, 3 or column 0, 1, 2, 3)

In this post, we’ll go over the basic syntax, as well as how to use boolean operators with loc and iloc so you can slice and dice your data as you need, as quickly as you need to.

Basic Syntax: df.loc[] vs. df.iloc[]

We’re using a subset of this Netflix dataset from Kaggle. We’ve set the index to be the show_id column as well.

df2.head()

Output:

First 5 rows of Netflix dataset from KaggleFirst 5 rows of Netflix dataset from Kaggle

df.loc Example 1

# Select multiple rows for 1 column using df.loc
df2.loc["s5696":"s5981", "title"]

Output:

show_id
s5696           #Rucker50
s5278        #realityhigh
s5972            (T)ERROR
s5980    1 Chance 2 Dance
s5981       1 Mile to You
Name: title, dtype: string

df.iloc Example 1

# Select multiple rows for 1 column using df.iloc
df2.iloc[0:6, 1] # "title" is col1 in the df (with 0-indexing)

Output:

show_id
s5696                              #Rucker50
s5278                           #realityhigh
s5972                               (T)ERROR
s5980                       1 Chance 2 Dance
s5981                          1 Mile to You
s3315    100 Things to do Before High School
Name: title, dtype: string

df.loc Example 2

# Select multiple columns for 1 row using df.loc
df2.loc["s5980", "type":"director"]

Output:

type                   Movie
title       1 Chance 2 Dance
director          Adam Deyoe
Name: s5980, dtype: object

df.iloc Example 2

# Select multiple columns for 1 row using df.iloc
df2.iloc[3, 0:3]

Output:

type                   Movie
title       1 Chance 2 Dance
director          Adam Deyoe
Name: s5980, dtype: object

Note on indexing

From the above examples, you’ll notice that when slicing using the : operator, the results of loc[start:end] are inclusive of the ending row or column. By contrast, the iloc[start:end] results do not include the ending row or column. For both properties, you can always explicitly select every row or column you would like to include like: [row1, row2, row5], just make sure to use the labels or integer positions depending on the property.

Using boolean expressions with loc and iloc

You can also subset your data by using one or more boolean expressions, as below. Note that the syntax is slightly different:

  • You can pass a boolean expression directly into df.loc.
  • When using df.iloc, you must first convert the results of the boolean expression or expressions into a list

The following two code snippets yield the same output.

df.loc Example with boolean expression

# Select rows based on boolean condition
df2.loc[(df2.release_year > 2000) & (df2.release_year < 2002), ["title", "director", "rating"]]

df.iloc Example with boolean expression

# Get rows that meet condition
bool_rows = list((df2.release_year > 2000) & (df2.release_year < 2002))

# Equivalent statement using iloc
df2.iloc[bool_rows, [1, 2, 7]]

Output:

pandas loc vs iloc outputpandas loc vs iloc output

About

Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit www.einblick.ai and follow us on LinkedIn and Twitter.

Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators