Here’s the tutorial on how to reset index in Pandas data frame.
It’s not hard to imagine a situation where you need to reset an index on your dataframe. The most common example is to extract data from a larger data set. In this case, the index may not match and you just need to fix it so that you can continue working with the data table smoothly.
How to reset index
There are several methods to reset the index, including:
- df.reset_index(): This method resets the index to default integers starting from 0 and adds the old index values as a new column in the DataFrame. To reset the index in-place, you can pass the argument inplace=True.
- df.set_index(): This method sets the DataFrame index to a specified column. By passing inplace=True and drop=True, you can reset the index and remove the original index column from the DataFrame.
- df = df.loc[df.index.dropna()]: This method removes any rows with missing values in the index and resets the index to default integers starting from 0.
To reset the index to the default integer index starting at 0, you can simply use the reset_index() function provided by the Pandas library. This is the easiest way to reset DataFrame index.
This is my dataframe. I specifically entered the indexes that are out of order. I don’t like this indexing and would like to fix it.
import pandas as pd my_df = pd.DataFrame({'Column1': [3, 5, 7, 4, 8, 5], 'Column2': [2, 7, 5, 3, 7, 4], 'Column3': [3, 7, 5, 3, 8, 6]}, index=[4, 2, 3, 1, 0, 5]) print(f"This my data frame with an index I do not like\n{my_df}")
To reset the index I use the reset_index function.
import pandas as pd my_df = pd.DataFrame({'Column1': [3, 5, 7, 4, 8, 5], 'Column2': [2, 7, 5, 3, 7, 4], 'Column3': [3, 7, 5, 3, 8, 6]}, index=[4, 2, 3, 1, 0, 5]) my_df.reset_index(inplace=True) print(f"This my data frame with an index I do like\n{my_df}")
Note that you can also specify the name of the new index column by passing the argument level or name.
The reset_index function has many parameters. I only used the inplace parameter which overwrote my source table. Other parameters are used to handle more advanced cases, including multiindex, which I will discuss in separate articles.
For more details just read the documentation of a reset_index function.
Pingback: How To Remove Index • Pandas How To
Pingback: How To Resolve ValueError: Index Has Duplicate Keys Error? • Pandas How To
Pingback: How To Drop All Columns Except One In Pandas • Pandas How To
Pingback: How To Set Index • Pandas How To
Pingback: How To Set Multiindex • Pandas How To