Table of contents
1.
Introduction
2.
What is the loc function in Python
2.1.
Syntax
3.
Use Cases of Loc Function
3.1.
Code Implementation
3.1.1.
Output
3.2.
Selecting A Single Row
3.2.1.
Code Implementation
3.2.2.
Output
3.3.
Explanation 
3.4.
Selecting Multiple Rows
3.4.1.
Code Implementation
3.4.2.
Output
3.4.3.
Explanation 
3.5.
Selecting Multiple Columns
3.5.1.
Code Implementation
3.5.2.
Output
3.5.3.
Explanation 
3.6.
Selecting A Subset Of Rows And Columns
3.6.1.
Code Implementation
3.6.2.
Output
3.6.3.
Explanation 
3.7.
Selecting A Value
3.7.1.
Code Implementation
3.7.2.
Output
3.7.3.
Explanation 
3.8.
Selecting Multiple Values
3.8.1.
Code Implementation
3.8.2.
Output
3.8.3.
Explanation 
4.
Advantages of loc
5.
Disadvantages of loc
6.
What is the iloc function in Python?
6.1.
Syntax
7.
Use Cases Of iloc() Function
7.1.
Code Implementation
7.2.
Selecting Specific Rows And Columns
7.2.1.
Code Implementation
7.2.2.
Output
7.3.
Updating Values
7.3.1.
Code Implementation
7.3.2.
Output
7.4.
Slicing
7.4.1.
Code Implementation
7.4.2.
Output
7.4.3.
Advantages of iloc
7.4.4.
Disadvantages of iloc
8.
Difference Between loc and iloc
9.
Frequently Asked Questions
9.1.
When should we use loc instead of iloc from a Pandas DataFrame?
9.2.
What happens if you use a non-unique label when using loc to select data?
9.3.
What happens if you pick rows or columns in a Pandas DataFrame using a negative integer with iloc?
9.4.
How are missing values handled when the developer is using loc and iloc functions?
9.5.
Where do you use loc and iloc functions?
10.
Conclusion
Last Updated: Feb 5, 2025
Easy

Difference Between loc() and iloc() in Pandas

Author Vidhi Sareen
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Data analysis and management are essential in every data-driven company. Python Pandas library has excellent functions to perform different tasks. The Pandas library's main objective is to select rows and columns from a dataset according to our needs. This dataset may contain text or numerical data. 

The two most essential and Must Know Functions in pandas library has are "loc" and "iloc." In this article, we will discuss what "loc and "iloc" are. We will explore different aspects like the difference between loc and iloc features, and how it works in different circumstances.

difference between loc and iloc

What is the loc function in Python

"Loc" is a method in the Pandas library of Python. It is both a dataframe and series method which means we can call it on either pandas' objects. Dataframe can be referred to as a spreadsheet or a table. It consists of columns representing a variable and row as an observation. There are two arguments we need to pass when we are using this function. The first arguments represent the row label and the second argument represents a column label. We can even use colon (:) if we want to select all rows or columns. We use boolean expressions to solve it.

Syntax

We have to follow the syntax below:

Dataframe.loc[specific rows, specific columns]

Use Cases of Loc Function

To understand it more in-depth, let's look at an example. Let's create a sample Dataframe using Pandas: 

Code Implementation

//import python library 
Import pandas as pd

df = pd.DataFrame({
‘Name’ : [‘Ninja_1’,‘Ninja_2’,‘Ninja_3’,‘Ninja_4’,
‘Department’ : [‘Marketing’,’Sales’,’Marketing,’Engineering’],
‘Salary’ : [50000,60000,55000,7000]
//create a sample DataFrame with attributes name, department and salary.
})                                                    
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_1 Marketing 50000
Ninja_2 Sales 60000
Ninja_3 Marketing 55000
Ninja_4 Engineering 70000

We have created a sample DataFrame which consists of 3 columns named 'name,' 'Department,' and' Salary.' 

There are different use cases like:

Selecting A Single Row

Code Implementation

Row = df.loc[2]
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_3 Marketing 55000

Explanation 

As you can see, the code selected the row that is labeled with “2”. This is indicating the third row in the DataFrame since Python uses 0-based indexing technique. 

Selecting Multiple Rows

Code Implementation

Row = df.loc[[1, 2, 3]]
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_2 Sales 60000
Ninja_3 Marketing 55000
Ninja_4 Engineering 70000

Explanation 

As you can see, the rows with labels '1','2','3' are selected. Here it is indicating towards the second, third and fourth rows in DataFrame since Python uses 0-based indexing technique.

Selecting Multiple Columns

Code Implementation

Cols = df.loc[:, [‘Name’,’Salary’]]
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Salary
Ninja_1 50000
Ninja_2 60000
Ninja_3 55000
Ninja_4 70000

Explanation 

As you can see, it is pointing towards the all rows and “name” and “Salary” column since Python uses 0-based indexing technique.

Selecting A Subset Of Rows And Columns

Code Implementation

Subset = df.loc[df [‘Department’] == ‘Marketing’, [‘Name’,’Salary’]]
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_1 Marketing 50000
Ninja_3 Marketing 55000

Explanation 

It is indicating to all rows where the value in the “Department” is Marketing. And also include the “Name” and “Salary” column since Python uses 0-based indexing technique.

Selecting A Value

Code Implementation

value = df.loc[1, ‘Salary’] = 65000
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_1 Marketing 50000
Ninja_2 Sales 65000
Ninja_3 Marketing 55000
Ninja_4 Engineering 70000

Explanation 

In this the value in 'Salary' column is getting updated to 65000 for row label with “1” since Python uses 0-based indexing technique.

Selecting Multiple Values

Code Implementation

value_1 = df.loc[[0,3],’Salary’] = 60000
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_1 Marketing 60000
Ninja_2 Sales 65000
Ninja_3 Marketing 55000
Ninja_4 Engineering 60000

Explanation 

In this the value in 'Salary' column is getting updated to 60000 for row labels with “0” and “3” since Python uses 0-based indexing technique.

We can see that many operations can be held using the "loc" method.

Advantages of loc

Here are a few advantages of loc.

  • Allowed in cases like labeled-based indexing. It is easy to read and understand.
     
  • It can be used with Boolean arrays to solve problems.
     
  • Can be used on both single and multiple indexes.

Disadvantages of loc

Some of the disadvantages of loc are as follows:

  • It can be low with large DataFrames.
     
  • It uses unique indexes, but it may sometimes be different.

What is the iloc function in Python?

The iloc function in Python is an index-based function. In this function, we select an integer position instead of selecting rows or columns. It can also work across multiple DataFrame Objects.

"iloc" method is a valuable tool for selecting rows and columns by an integer. It can also access specific values in a DataFrame.It does not accept the boolean data. We have to follow the syntax below:

Syntax

df.iloc[row_index_value, column_index_value]
You can also try this code with Online Python Compiler
Run Code

Use Cases Of iloc() Function

To understand it more in-depth, let's look at an example. Let's create a sample Dataframe using Pandas:

There are different use cases like:

Code Implementation

//import python library
Import pandas as pd

#create a sample DataFrame
df = pd.DataFrame({
‘Name’ : [‘Ninja_1’,‘Ninja_2’,‘Ninja_3’,‘Ninja_4’,
‘Department’ : [‘Marketing’,’Sales’,’Marketing,’Engineering’],
‘Salary’ : [50000,60000,55000,7000]
//create a sample DataFrame with attributes name, department and salary
})
You can also try this code with Online Python Compiler
Run Code

Selecting Specific Rows And Columns

Code Implementation

row = df.iloc[[1,3], [0,2]]
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Salary
Ninja_2 60000
Ninja_4 70000

Updating Values

Code Implementation

updated_value = df.iloc[1,2] =100
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department Salary
Ninja_1 Marketing 50000
Ninja_2 Sales 100
Ninja_3 Marketing 55000
Ninja_4 Engineering 70000

Slicing

Code Implementation

s = df.iloc[0:3, 0:2]
You can also try this code with Online Python Compiler
Run Code

 

Output

Name Department
Ninja_1 Marketing
Ninja_2 Sales
Ninja_3 Marketing

‘iloc’ method in Pandas is a valuable tool for selecting rows and column values using integer values. Many operations can be held using the 'iloc' method. In the article, We will also discuss the difference between 'loc' and 'iloc' and the key advantages and disadvantages of 'loc' and 'iloc.'

In the article further, we will discuss the advantages and disadvantages of loc and iloc functions. We will also discuss the difference between 'loc' and 'iloc.'

Advantages of iloc

Some of the advantages of iloc are as follows:

  • Allowed in cases like integer-based indexing.
     
  • More effective for large DataFrames.
     
  • It uses integer arrays to solve complex problems.
     
  • Can be used on both single and multiple indexes.

Disadvantages of iloc

Some of the disadvantages of iloc are as follows:

  • It's hard to read and understand for new users.
     
  • It can create problems if not updated regularly and lead to data misinterpretation.

So we can say that it depends on the uses' needs. But in general, 'loc' is used for label-based indexing, and 'iloc' is used for integer-based indexing.

Difference Between loc and iloc

loc function Iloc function
Select rows and columns by labels Select rows and columns by integer positions
Slicing with labels Slicing with integer positions
Use Boolean arrays Does not uses Boolean arrays
Label-based indexing Position based indexing
Syntax : Dataframe.loc[specific rows, specific columns] Syntax : Dataframe.iloc[row_index_value, column_index_value]

Example:

df.loc[2, 'Salary']

df.loc[df['Department'] == 'Marketing', ['Name', 'Salary']]

Example:

df.iloc[2, 2]

df.iloc[1:3, :2]

Frequently Asked Questions

When should we use loc instead of iloc from a Pandas DataFrame?

To select data based on label-based indexing, it is recommended to use loc. And iloc should be used when there is a need to choose data that is classified via integer-based indexing.

What happens if you use a non-unique label when using loc to select data?

Using loc with a non-unique label will throw a KeyError exception. This is because loc assumes that the labels used for indexing are unique and will return a single row or column. We should use Boolean indexing to select more than one row or column where duplicates exist.

What happens if you pick rows or columns in a Pandas DataFrame using a negative integer with iloc?

If there is a negative integer used with 'iloc', it will start counting rows and columns from the end of the DataFrame. For example if you want to select the last row in the DataFrame then you can use ‘-1’.

How are missing values handled when the developer is using loc and iloc functions?

They are simply removed from the selection. If you want to remove missing values you can use fillna() or dropna() function which will either remove or fill missing values with null.

Where do you use loc and iloc functions?

Both of the functions are used in data analysis and data manipulation tasks. They are used in data filtering, data transformation or in data aggregation.

Conclusion

In conclusion, the 'loc' function is mainly used when we want to select rows and columns based on their labels. Whereas 'iloc' is used when choosing rows and columns based on specific positions.

Recommended Reading:

Difference Between IOT and M2M

The main difference we saw is that in 'loc,' we include start and end positions, but it is different with 'iloc'. We have analyzed the difference between loc and iloc for effective indexing and selecting data in DataFrame.
Also, try Coding Ninjas Studio to practice programming problems for your complete interview preparation. Also, check out how to install Pandas in Python and top pandas interview questions and answers to get hands-on experience with frequently asked interview questions and land your dream job.

“Happy coding”

Live masterclass