DataFrame Index

The index is the ordered sequence of row labels a DataFrame carries; labels need not be unique. A table constructed from lists without an explicit index usually gets 0, 1, 2 and so on. Filtering and sorting preserve labels while changing which positions they occupy. Labels therefore identify rows only as well as the data contract behind them.

loc selects labels and iloc selects positions. A valid label slice includes both bounds; a positional slice excludes its stop. Unsorted or duplicate labels can make some label slices ambiguous or invalid. For conditional source updates, use one .loc[rows, column] assignment. Direct column assignment and scalar .at/.iat writes are also valid; loc is not the only reliable write API.

The index also decides alignment. Arithmetic between two objects, assigning a Series into a column, and concatenating along columns all match labels rather than positions, so two extracts sorted differently combine correctly by label and incorrectly by any assumption about order. Stacking tables keeps the original labels and can repeat them, which makes label-based selection ambiguous; ignore_index=True during a concatenation or reset_index(drop=True) afterwards renumbers them.

Setting a meaningful index with set_index() is worth it when rows have a natural identifier and you look them up by it, and it is what a grouped result gives you automatically, with the group keys as labels. Selecting columns by number is the fragile counterpart: insert a column upstream and every position shifts silently. Name columns you know; keep positional access for questions that are genuinely about position, such as the first few rows.

References: pandas indexing and selecting data, pandas MultiIndex and advanced indexing. See it in use in Pandas Foundations: Tables, Filtering, Grouping, and Joins.


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.