6. Chained Indexing

Chained indexing is whenever two or more subset selections immediately following each other.

Not Recommended!

This can lead to unexpected behavior and is generally not recommended because it may not always return a view or copy of the DataFrame, potentially causing issues with data modifications.

Example:

>>> a = [1, 5, 10, 3, 99, 5, 8, 20, 40]  
>>> a  

[1, 5, 10, 3, 99, 5, 8, 20, 40]

Let’s make a normal subset selection by slicing from integer location 2 to 6.

>>> a[2:6] 

[10, 3, 99, 5]

Chained indexing occurs whenever we make another subset selection immediately following this one.

Let’s select the first element from this new list in a single line of code:

>>> a[2:6][0]  

10
Powered by Forestry.md