liveBook · Manning

Reset_index

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

reset_index is a pandas method that replaces an object's index with a default integer index and can optionally preserve the old index as a column.

reset_index replaces a DataFrame's current index with a default integer index. It is useful after filtering, concatenating, grouping, or creating an index from a data column. section 6.12 section 11.18

Choose whether the displaced index should remain available:

  • Use reset_index() when the old index should become a regular column. section 6.12
  • Use reset_index(drop=True) when the old index is no longer useful and should be discarded. section 11.16 section 11.18
  • Check df.index.is_unique when plotting tools may reject repeated index values. section 11.18

Prepare concatenated data for plotting

After combining January and July taxi data, check the index and renumber the rows: df.index.is_unique; df = df.reset_index(drop=True). The resulting DataFrame has unique default row numbers, allowing Seaborn relationship and line plots to proceed without index-related errors. section 11.18 section 11.16

df = df.reset_index(drop=True)

Run the concise reset_index(drop=True) operation used to replace an unsuitable index before visualization. section 11.16

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains default renumbering and drop=True, but does not detail the complete pandas API.
Examples
Uses locations data for joining and concatenated weather and taxi data for repairing indexes before plots.
What it assumes
Assumes familiarity with pandas DataFrames, indexes, filtering, concatenation, and basic plotting.
What you can do after
Apply it via pandas to create default row numbers, preserve an old index, or discard it before plotting.
How it connects
Connects indexes with DataFrame joins, concatenation, grouping, filtering, and Seaborn visualization.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.