set_index()
noun
say it: “set IN-deks” /ˈsɛt ˈɪnˌdɛks/
A pandas method that turns one of a table's columns, such as a product code, into the labels of its rows.
What does set_index() do in pandas?
set_index() makes a column, such as a product code, the table's row labels (its index), and hands back the new
table. loc can then find a row by that label, and reset_index() turns the labels back into an ordinary column.
catalog = products.set_index("sku")
print(catalog.loc["TO-1042", "product"])
In a sentence
- “set_index() made the sku column the row labels.”
- “After set_index(), loc found each product by its code.”
Level: Journeyman · More Python terms at this level
Updated