The Index is the backbone of relational alignment in Pandas. It provides unique labels that identify each row, enabling instant lookups, joins, and time-series resampling.
By default, when creating or loading a DataFrame, Pandas assigns a default zero-indexed integer range: RangeIndex(0, 1, 2, ...).
set_index)When a column contains unique identifiers (like Employee_ID, Roll_No, or Timestamp), promoting it to the Index provides faster query speeds and cleaner semantics:
Output:
Now row lookups by employee ID are immediate:
reset_index)After filtering, sorting, or grouping, the index labels may become jumbled or discontinuous (e.g., rows 3, 7, 12). reset_index() restores a clean 0, 1, 2, ... index:
drop=True)If you filter rows and do not want the old index saved as a new column:
Pandas allows DataFrames to have multiple index levels, enabling high-dimensional data representation in a 2D table:
df.set_index('Customer_ID') method do?df.reset_index(drop=True)?df.set_index('col', inplace=True) to modify the original DataFrame directly?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Boolean Indexing & DataFrame Querying | Detecting Missing Values (isna, notna, isnull) |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.