Call df = df.reset_index(drop=True) after filtering, sorting, dropping rows, or concatenating DataFrames to discard the old fragmented index and replace it with a clean, sequential 0, 1, 2, ... RangeIndex—omitting drop=True inserts the old index as an extra index column instead of throwing it away.

Basic Example

import pandas as pd

df = pd.DataFrame(
    {"city": ["Lahore", "Karachi", "Islamabad", "Multan"], "score": [88, 92, 79, 95]},
    index=[10, 25, 30, 42],
)

# Filter rows (leaves non-contiguous index [25, 42]):
filtered = df[df["score"] > 90]

# 1. Recommended: Replace old index with a clean 0..N-1 RangeIndex (discard old index)
clean_df = filtered.reset_index(drop=True)
print(clean_df)
# Output:
#       city  score
# 0  Karachi     92
# 1   Multan     95

# 2. Omit `drop=True` ONLY when you want to preserve a meaningful index as a regular column:
kept_index_df = filtered.reset_index()
print(kept_index_df)
# Output:
#    index     city  score
# 0     25  Karachi     92
# 1     42   Multan     95

Key Details & Edge Cases

  • drop=True vs. drop=False (the default):
    • By default (drop=False), df.reset_index() moves the current index into a new column named "index" (or its .name attribute if named) and assigns a new RangeIndex. Always pass drop=True when the old row labels are just stale row numbers from filtering or sorting.
  • Avoid ValueError: cannot insert index, already exists:
    • If df already contains a column literally named "index" (for example, from calling .reset_index() twice without drop=True!), a second .reset_index() without drop=True raises ValueError: cannot insert index, already exists. Pass drop=True, or rename the index first via df.rename_axis("row_id").reset_index() (or names="row_id" in Pandas 1.5+).
  • Avoid inplace=True in modern Pandas (df = df.reset_index(drop=True)):
    • Reassigning df = df.reset_index(drop=True) enables clean method chaining (df.dropna().sort_values("score").reset_index(drop=True)), avoids SettingWithCopyWarning on sliced DataFrames, and aligns with Pandas 2.x/3.0 Copy-on-Write best practices where inplace=True provides no memory or speed advantage.
  • Avoid redundant .reset_index(drop=True) during pd.concat():
    • When stacking DataFrames vertically with pd.concat([df1, df2]), pass ignore_index=True (pd.concat([df1, df2], ignore_index=True)) to build a clean 0..N-1 RangeIndex directly in one step instead of concatenating duplicate indices and calling .reset_index(drop=True) afterward.
  • Resetting specific levels of a MultiIndex (level=...):
    • If df has a multi-level index (e.g., ["region", "year"]) and you only want to reset one level while keeping the other as the index, pass level="year" (df.reset_index(level="year", drop=False)).

Comparison of Index-Resetting Patterns in Pandas

Pattern Keeps Old Index as a Column? Works on MultiIndex? Supports Method Chaining? Best Used When
df = df.reset_index(drop=True) (Recommended) No (discards old index completely) Yes (drops all levels) Yes Cleaning up row numbers after filtering (df[...]), sorting (.sort_values()), or .dropna().
df = df.reset_index() (or names="id") Yes (inserts as "index" or custom column name) Yes (converts all levels to columns) Yes Promoting a meaningful index (such as dates, IDs, or GroupBy keys) into a regular column.
pd.concat([df1, df2], ignore_index=True) No (generates fresh 0..N-1 RangeIndex) Yes Yes Combining multiple DataFrames vertically without creating duplicate index labels first.
df.reset_index(level="lvl", drop=True) No (for specified level only) Yes (targets specific level(s)) Yes Dropping or promoting a single level of a hierarchical MultiIndex while preserving outer levels.