pandas reset_index() – Rest Index on DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 18, 2022

DataFrame.reset_index() method is used to reset the index on the pandas DataFrame. This takes level, drop, inplace, col_level, col_fill as parameters and returns DataFrame. When inplace=True is used, it returns None. Use pandas.DataFrame.set_index() to set column as index.

In this article, I will cover how to reset index on pandas DataFrame with several examples. We are usually required to reset the index post dropping some rows from DataFrame as it creates a gap in the index.

1. Quick Examples of pandas reset index

Below are some quick examples of reset index on DataFrame.


# Quick examples of reset index
# Reset index
df2=df.reset_index()

# Reset index with out assigning to column
df.reset_index(inplace=True, drop=True)

# set index column name
df.index.name = 'custom_index'

# Reset index starting from 1
df.index = np.arange(1, len(df) + 1)

2. pandas.DataFrame.reset_index() Syntax

Following is the syntax of the pandas.DataFrame.reset_index().


DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
  • level – Takes int, str, tuple, or list, default None
  • dropbool, default False. When set to True, ignores setting old index as column.
  • inplacebool, default False. When set to True, it updates the existing DataFrame object.
  • col_level – If the columns have multiple levels, determines which level the labels are inserted into. 
  • col_fill – If the columns have multiple levels, determines how the other levels are named.

Let’s create a pandas DataFrame with examples


import pandas as pd

# Create DataFrame from dict
df = pd.DataFrame({'Courses':['Spark','PySpark','Java','PHP'],
           'Fee':[20000,20000,15000,10000],
           'Duration':['35days','35days','40days','30days']})

df=df.drop([2])
print(df)

Yields below output


   Courses    Fee Duration
0    Spark  20000   35days
1  PySpark  20000   35days
3      PHP  10000   30days

3. pandas Rest Index on DataFrame Example

Let’s use the pandas.DataFrame.reset_index() syntax to reset the DataFrame index. When we reset the index, the old index is added as a column to DataFrame, and a new sequential index is used


# reset index on DataFrame
df2=df.reset_index()
print(df2)

Yields below output.


   index  Courses    Fee Duration
0      0    Spark  20000   35days
1      1  PySpark  20000   35days
2      3      PHP  10000   30days

If you don’t want to add the existing index as a column, use drop=True param and also use inplace=True to update the existing DataFrame.


# drop index as column
df.reset_index(inplace=True, drop=True)
print(df)

Yields below output


   Courses    Fee Duration
0    Spark  20000   35days
1  PySpark  20000   35days
2      PHP  10000   30days

4. Reset index name on DataFrame

Now, let’s see how to reset an index name on pandas DataFrame. By default, DataFrame creates with index name as ‘index’


# Set index name
df.index.name = 'custom_index'
print(df)

Yields below output.


              Courses    Fee Duration
custom_index                         
0               Spark  20000   35days
1             PySpark  20000   35days
2                 PHP  10000   30days

5. Reset Index to Start from 1

When you reset the index it assigns a numerical value starting from zero, some times you may need to assign from 1, you can do this as shown in the below example.


# Reset index starting from 1
df.index = np.arange(1, len(df) + 1)
print(df)

Yields below output


   Courses    Fee Duration
1    Spark  20000   35days
2  PySpark  20000   35days
3      PHP  10000   30days

6. Complete Example of Reset Index on DataFrame


import pandas as pd
import numpy as np

# Create DataFrame from dict
df = pd.DataFrame({'Courses':['Spark','PySpark','Java','PHP'],
           'Fee':[20000,20000,15000,10000],
           'Duration':['35days','35days','40days','30days']})

df=df.drop([2])
print(df)

# Reset index
df2=df.reset_index()
print(df2)

# Reset index with out assigning to column
df.reset_index(inplace=True, drop=True)
print(df)

# set index column name
df.index.name = 'custom_index'
print(df)

# Reset index starting from 1
df.index = np.arange(1, len(df) + 1)
print(df)

Conclusion

In this article, you have learned how to reset the index on DataFrame by using different prams on the reset_index() method.

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing pandas reset_index() – Rest Index on DataFrame