In this blog, we will explore the reasons why Python Pandas includes the loc and iloc indexers.
The Pandas Series can be initiated in 2 ways:
explicit index (user-defined)
data = pd.Series([10, 20, 30, 40], index=[4, 3, 8, 2]) data # -------------- OUTPUT -------------- # 4 10 3 20 8 30 2 40 dtype: int64
implicit Python-style index (auto-generated)
data = pd.Series([10, 20, 30, 40]) data # -------------- OUTPUT -------------- # 0 10 1 20 2 30 3 40 dtype: int64
How to access the item at an index?
If the Series has an explicit integer index, an indexing operation such as
data[1]
will utilize explicit indices.If the Series has an implicit integer index, the indexing operation will fall back to using the implicit Python-style indices.
What happens when we perform a slicing operation like data[1:3]
on an explicit index Series?
In this case, the slicing operation will always use implicit Python-style indices (even when it has an explicit integer index)
data = pd.Series([10, 20, 30, 40], index=[4, 3, 8, 2])
print(data[1:3])
# -------------- OUTPUT -------------- #
3 20
8 30
dtype: int64
Because of this potential confusion in the case of integer indexes, Pandas provides special indexer attributes - loc and iloc, that explicitly expose certain indexing schemes.
Using loc for Explicit Indexing
The loc indexer is designed to facilitate indexing and slicing operations that explicitly reference the user-defined index. By using loc, we ensure that the explicit index is always utilized, regardless of whether it is a numeric or non-numeric index. This consistency in indexing helps maintain clean and readable code.
Using iloc for Implicit Indexing
On the other hand, the iloc indexer focuses on indexing and slicing operations that utilize the implicit Python-style index. Even if a Series has an explicit integer index, the iloc indexer ensures that the slicing operation always employs the implicit index. This consistency eliminates potential bugs that could arise due to mixed indexing/slicing conventions.
data = pd.Series([10, 20, 30, 40], index=[4, 3, 8, 2])
print(data.loc[3])
# -------------- OUTPUT -------------- #
20
print(data.iloc[3])
# -------------- OUTPUT -------------- #
40
Conclusion
The loc and iloc indexers in Pandas provide explicit access to different indexing schemes. loc ensures that indexing and slicing operations always reference the explicit index, while iloc utilizes the implicit Python-style index. By consistently using loc and iloc, we can avoid confusion and maintain clean, readable code.