Search code examples
pythonpandas

Python Pandas: Does 'loc' and 'iloc' stand for anything?


I've been using pandas for a while now, I understand what loc and iloc do. But till this day I don't know if these two things stand for something? Are they short for something or abbreviations? Or are they just random?

I interpret iloc as 'index-based-location' which makes sense, but loc is a bit problematic to me, I interpret it as 'location' but it doesn't shout 'label-based-location', why couldn't they call it lloc?


Solution

  • TLDR

    It seems there is no abbreviation semantically or in the docs; other than it really is just in lamens: "location" vs "integer location". Or Human-Readable Labels vs Computer-Logical Indexing.

    This happens to everyone, especially when dealing with new or complex languages and concepts. You understand what something does and how to use it, but when you try to rationalize its meaning or explain it to yourself, it feels unsettling.

    It’s both a programmer’s nightmare and a dream all in one.


    The way I see it, imagine you’re working at a department store, and a customer asks where to find the Dr. Pepper. You could tell them "it’s in the soda aisle" or versus "it’s in aisle 19". (Or: aisles["soda"] vs. aisles[19].) Both refer to the same location.

    Expanded Discussion


    To properly answer your question, you're asking "Does loc and iloc stand for anything?" rather than "What is the difference between loc and iloc?"

    I looked into this and found some relevant discussions. A GitHub issue led me to this summary. Based on my research, I believe it can be summarized as follows:

    Different Choices for Indexing

    Object selection has had a number of user-requested additions in order to support more explicit location based indexing.

    • .loc: is primarily label based
    • .iloc: is primarily integer position based

    And on the chance we want to include ix

    • .ix supports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access continue

    Selection By Label

    pandas provides a suite of methods in order to have purely label based indexing... - continued

    • The .loc attribute is the primary access method. ↑

    Selection By Position

    pandas provides a suite of methods in order to get purely integer based indexing...

    • continued

    • The .iloc attribute is the primary access method. ↑

    This applies to .at and .iat as well

    Similarly to loc, at provides label based scalar lookups, while, iat provides integer based lookups analogously to iloc

    By the way I retracted my close vote and gave you an upvote as that did take some guts to ask for more clarification on an already over asked topic but I do know I as well had issues with that when I was learning too. Hope this helps