Difference between Pandas .iloc and .loc function

The optimized data access methods are accessed by indexing off of the .loc and .iloc attributes. These two attributes allow label-based and position-based indexing respectively.

When we perform an index operation on the .iloc attribute, it does lookup based on index position (in this case pandas behaves similar to a Python list).

DataFrame operation:

.loc is supposed to be based on the index labels and not the positions. As such, it is analogous to Python dictionary-based indexing. Though it has some additional functionality, as it can accept boolean arrays, slices, and a list of labels (none of which work with a Python dictionary):

Series operation:

If you get confused by .loc and .iloc, remember that .iloc is based the index (starting with i) position. .loc is based on label (starting with l).

Popular Posts

Spread the knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *