Pandas – How to reset index in a given DataFrame

How about we examine how to reset list in Pandas DataFrame. Regularly We start with a tremendous dataframe in Pandas and subsequent to controlling/sifting the dataframe, we end up with a lot more modest dataframe.

At the point when we check out the more modest dataframe, it may in any case convey the line list of the first dataframe. Assuming the first file are numbers, presently we have lists that are not ceaseless. All things considered, pandas has reset_index() work. So to reset the list to the default whole number list starting at 0, We can basically utilize the reset_index() function. By the finish of this article, you will know the various highlights of reset_index work, the boundaries which can be redone to get the ideal result from the capacity. This likewise covers use cases that are firmly connected with doing reset record in pandas.

Allow us to perceive how to reset the file of a DataFrame in the wake of dropping a portion of the lines from the DataFrame.

Approach:

  • Import the Pandas module.
  • Make a DataFrame.
  • Drop a few lines from the DataFrame utilizing the drop() technique.
  • Reset the record of the DataFrame utilizing the reset_index() strategy.
  • Show the DataFrame after each progression.
# importing the modules
import pandas as pd
import numpy as np
# creating a DataFrame
ODI_runs = {'name': ['Tendulkar', 'Sangakkara', 'Ponting',
                      'Jayasurya', 'Jayawardene', 'Kohli',
                      'Haq', 'Kallis', 'Ganguly', 'Dravid'],
            'runs': [18426, 14234, 13704, 13430, 12650,
                     11867, 11739, 11579, 11363, 10889]}
df = pd.DataFrame(ODI_runs)
# displaying the original DataFrame
print("Original DataFrame :")
print(df)
# dropping the 0th and the 1st index
df = df.drop([0, 1])
# displaying the altered DataFrame
print("DataFrame after removing the 0th and 1st row")
print(df)
# resetting the DataFrame index
df = df.reset_index()
# displaying the DataFrame with new index
print("Dataframe after resetting the index")
print(df)

Output:

Also ReadHow to Encrypt and Decrypt Strings in Python?

Leave a Reply

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