Pandas reset index – How to reset index Pandas

reset index in pandas dataframe and remove old index

In this post, I will show you how to reset index in Pandas DataFrame in Python. Often after doing data manipulation in pandas dataframe the manipulated data may contain row index name of the original dataframe.

We can use python pandas.reset_index() function to set index to the default integer index starting at 0.

pandas.reset_index

Syntax

  • DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='', allow_duplicates=_NoDefault.no_default, names=None)

Parameters

  • level: str, int, tuple or list (default None)
    • Only remove the specified levels from the index. By default removes all levels
  • drop: bool (default False)
    • Used for resetting the index to it’s default integer index
  • inplace: bool (default False)
    • Whether to modify the DataFrame instead of creating a new one
  • col_level: str or int (default 0)
    • if the columns contain multiple levels, determines the level at which the labels will be inserted. It is inserted into the first level by default.
  • col_fill: object (default ")
    • if the columns contain multiple levels, determine the name of other levels. If value is None then the index name will be repeated
  • Returns: DataFrame containing the new index or None if inplace=True

How to reset index in pandas dataframe

Create a sample DataFrame
import pandas as pd
import numpy as np
import random

# Create dataframe with index

df = pd.DataFrame({
                    'Physics': [85, 74, 63, 88, 91],
                    'Chemistry': [69, 83, 73, 50, 61],
                    'Mathematic': [74, 82, 61, 72, 65]
                  }, index=['Rajesh', 'Nitesh', 'Neha', 'Sivam', 'Arti'])
df
create dataframe
Case 1: Pandas reset index to start at 0
df.reset_index()
pandas reindex from 0

After applying reset.index() function, the index name will be converted to a column name (in our example, student names). The default name of the column will be index.

Case 2: Rename Pandas DataFrame Index

Now as you can see the name of the converted column is index, but we want that column name to be Student_Name.

# reset index to column in pandas
df.index.rename('Student_Name', inplace=True)
df
Rename Pandas DataFrame Index
Reset Index
df = df.reset_index()
df
reset index to column pandas
Case 2: pandas reset index to start at 1

Now if you want to start the index from 1 instead of 0

df.index = np.arange(1, len(df) + 1)
df
pandas reset index to start at 1

Conclusion

In this pandas set index tutorial, I explained different ways of resetting the index in the pandas library of python:

  • Reset index name in pandas
  • pandas reindex from 0
  • pandas resetting index to start at 1
  • pandas remove index column
Also Read:  Basics of python programming in 30 days

If you have any questions or suggestions regarding this article, please let me know in the comment section below.

Leave a Comment

Your email address will not be published. Required fields are marked *