Search code examples
pythonpandasdataframeterminology

Terminology for ordinal index versus dataframe index


I've set up a dataframe:

df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
                   'Age': [25, 30, 35],
                   'Location': ['Seattle', 'New York', 'Kona']},
                  index=([10, 20, 30]))

I ask the user to put in which row of the data they want to see. They input a 0, indicating the first row.

However, df.loc[0] does not refer to the first row. Instead, it doesn't exist, because the index only has the values 10, 20, and 30.

Is there terminology to distinguish these two types of indexes? The best I could come up with is ordinal indexes (for "What number row is this?") and dataframe indexes (for "What is the index of this row in the dataframe?").

To clarify, under my definitions, df.index(ordinal_index) == df_index.

Is there a standard terminology for this?


Solution

  • If you look at the documentation you've linked, pandas uses the term index label to describe what you call a dataframe index.

    It's more clearly explained in this documentation on indexing, where the term position is used to refer to what you call an ordinal index. (Note that some popular answers on Stack Overflow use the term location rather than position.)

    Note that this terminology might get confusing because:

    1. The df.index property is essentially a mapping from row positions to row labels. (And this issue is especially compounded since the positions and labels are usually equivalent for most dataframe rows.)
    2. The df.columns.get_loc(column_label) function returns the position of a column with the given column label (and yet is called get_loc, thus why some people use the term location instead of position).
    3. The df.loc property expects to be accessed with a label (not a position/location): e.g. df.loc[row_label, column_label]. It's the (now depreciated) df.iloc function that should be accessed with a position/location.

    (I'm answering my own question because I found an answer just before I posted the question - I've also updated this answer as I've found more accurate info.)