Unlocking the Power of Pandas: Understanding loc and iloc

Nitish pandey
Python’s Gurus
Published in
3 min readJun 6, 2024

--

Photo by Chris Curry on Unsplash

Understanding loc and iloc in python

When working with data in Python, the Pandas library is a go-to tool for many data scientists and analysts. It offers robust and flexible ways to manipulate data, among which loc and iloc are two of the most commonly used indexing methods. However, their functionalities can often be confusing for beginners. In this post, we'll delve into the differences between loc and iloc and see how each can be used effectively.

What is loc?

The loc method in Pandas is used for label-based indexing. This means you select data based on the labels of rows and columns. It allows for accessing a group of rows and columns by labels or a boolean array.

Key Features of loc:

  • Label-Based: Uses labels (names) to access data.
  • Slicing: You can slice data based on label ranges.
  • Inclusive: The slicing endpoint is included.
  • Boolean Arrays: Supports boolean array indexing.

Example Usage:

Consider the following DataFrame:

import pandas as pd
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data, index=['row1', 'row2', 'row3'])
print(df)

Output:

A  B  C
row1 1 4 7
row2 2 5 8
row3 3 6 9

Using loc to access data:

python
# Accessing a single value
print(df.loc['row2', 'B']) # Output: 5
# Accessing a row
print(df.loc['row1'])
# Output:
# A 1
# B 4
# C 7
# Name: row1, dtype: int64
# Accessing multiple rows and columns
print(df.loc[['row1', 'row3'], ['A', 'C']])
# Output:
# A C
# row1 1 7
# row3 3 9

What is iloc?

The iloc method in Pandas is used for integer-location based indexing. This means you select data based on the integer positions of rows and columns. It is particularly useful for accessing data by numerical index.

Key Features of iloc:

  • Integer-Based: Uses integer positions to access data.
  • Slicing: You can slice data based on integer ranges.
  • Exclusive: The slicing endpoint is excluded.
  • Precision: Allows precise selection by numerical index.

Example Usage:

Using the same DataFrame as above, we can access data with iloc:

# Accessing a single value
print(df.iloc[1, 1]) # Output: 5
# Accessing a row
print(df.iloc[0])
# Output:
# A 1
# B 4
# C 7
# Name: row1, dtype: int64
# Accessing multiple rows and columns
print(df.iloc[[0, 2], [0, 2]])
# Output:
# A C
# row1 1 7
# row3 3 9

Key Differences

While both loc and iloc are powerful methods for accessing data in Pandas, they serve different purposes and have different behaviors:

  1. Indexing Basis:

loc uses labels for indexing.

iloc uses integer positions for indexing.

2. Slicing Behavior:

  • loc includes the endpoint of the slice.
  • iloc excludes the endpoint of the slice.

3. Flexibility:

  • loc allows for more flexible slicing and boolean array indexing.
  • iloc is more rigid and precise, focusing on integer positions.

Choosing Between loc and iloc

The choice between loc and iloc depends on the context of your data manipulation tasks:

  • Use loc when you want to work with data by labels, which is often more intuitive and readable, especially when dealing with named indices.
  • Use iloc when you need to access data by specific integer locations, which is useful for programmatic access and when the position of the data is more important than its label.

Conclusion

Understanding the differences between loc and iloc is essential for efficient data manipulation in Pandas. Both methods have their unique strengths and use cases, and mastering them will significantly enhance your data handling capabilities.

Whether you are slicing data by labels with loc or by integer positions with iloc, these tools will empower you to navigate and manipulate your datasets with ease.

Happy coding!

linkedin:- www.linkedin.com/in/nitish-pandey-482281186

Youtube:- www.youtube.com/@Data_fanatic

Python’s Gurus🚀

Thank you for being a part of the Python’s Gurus community!

Before you go:

  • Be sure to clap x50 time and follow the writer ️👏️️
  • Follow us: Newsletter
  • Do you aspire to become a Guru too? Submit your best article or draft to reach our audience.

--

--