Pandas DataFrame.reset_index() Function
Python Pandas DataFrame.reset_index() function resets the index of the given DataFrame. It replaces the old index with the default index. If the given DataFrame has a MultiIndex, then this method removes all the levels.
pandas.DataFrame.replace_index()
Syntax
DataFrame.replace_index(level=None, drop=False, inplace=False, col_level=0, col_fill="")
parameter
level |
It is a parameter of integer, string, tuple or list type. If passed then the function will delete the passed levels. |
drop |
It is a boolean parameter. It specifies the index to be inserted in the DataFrame column. It resets the index to the default integer index. |
inplace |
It is a boolean parameter. It specifies whether to modify the given DataFrame or create a new one. |
col_level |
It is an integer or string type parameter. If the column has multiple levels, it tells at which level the label should be inserted. |
col_fill |
It is an object type parameter. If the column has multiple levels, it tells how the other levels should be named. |
Return Value
It returns the Dataframe with the new index, inplace=True
or None if .
Example Code: DataFrame.reset_index()
Method to Reset Dataframe Index
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.reset_index()
print("The Modified Data frame is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
The Modified Data frame is:
index Attendance Name Obtained Marks
0 0 60 Olivia 90
1 1 100 John 75
2 2 80 Laura 82
3 3 78 Ben 64
4 4 95 Kevin 45
The function returns a DataFrame with the new index.
If you don't want to see another index column then you can set the parameter drop= True
. It will reset the index to the default index column.
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.reset_index(drop= True)
print("The Modified Data frame is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
The Modified Data frame is:
Attendance Name Obtained Marks
0 60 Olivia 90
1 100 John 75
2 80 Laura 82
3 78 Ben 64
4 95 Kevin 45
Example Code: DataFrame.reset_index()
Method to Reset MultiIndex DataFrame Index
import pandas as pd
import numpy as np
index = pd.MultiIndex.from_tuples([(1, 'Sarah'),
(1, 'Peter'),
(2, 'Harry'),
(2, 'Monika')],
names=['class', 'name'])
columns = pd.MultiIndex.from_tuples([('Performance', 'max'),
('Grade', 'type')])
dataframe = pd.DataFrame([('Good', 'A'),
( 'Best', 'A+'),
( 'Bad', 'C'),
(np.nan, 'F')],
index=index,
columns=columns)
print("The Original Data frame is: \n")
print(dataframe)
dataframe1 = dataframe.reset_index(drop= True)
print("The Modified Data frame is: \n")
print(dataframe1)
Output:
The Original Data frame is:
Performance Grade
max type
class name
1 Sarah Good A
Peter Best A+
2 Harry Bad C
Monika NaN F
The Modified Data frame is:
Performance Grade
max type
0 Good A
1 Best A+
2 Bad C
3 NaN F
The function resets the index and adds a default integer index.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a
Pandas DataFrame.resample() Function
Publish Date:2025/05/01 Views:78 Category:Python
-
Python Pandas DataFrame.resample() function resamples time series data. pandas.DataFrame.resample() Syntax DataFrame . resample( rule, axis = 0 , closed = None , label = None , convention = "start" , kind = None , loffset = None , base = No
Pandas DataFrame.insert() Function
Publish Date:2025/05/01 Views:116 Category:Python
-
Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame. pandas.DataFrame.insert() Syntax DataFrame . insert(loc, column, value, allow_duplicates = False ) parameter loc It is an integer param
Pandas DataFrame.idxmax() Function
Publish Date:2025/05/01 Views:79 Category:Python
-
Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.
Pandas DataFrame sort_index() Function
Publish Date:2025/05/01 Views:183 Category:Python
-
This tutorial explains how to use pandas.DataFrame.sort_index() the sort method to sort a Pandas DataFrame based on its index. We will use the DataFrame shown in the above example to explain how to sort a Pandas DataFrame based on the index
Pandas cut function
Publish Date:2025/05/01 Views:165 Category:Python
-
pandas.cut() The function can distribute the given data into a range, which can also be called bins . We will use the following DataFrame in this article. import pandas as pd df = pd . DataFrame( { "Name" : [ "Anish" , "Birat" , "Chirag" ,
Appending to an Empty DataFrame in Pandas
Publish Date:2025/05/01 Views:54 Category:Python
-
As we learned earlier, Pandas in Python is an open source module that we can use for data analysis and making machine learning models. It is Numpy used along with another package called as they go hand in hand to support multidimensional ar
Pandas DataFrame DataFrame.query() function
Publish Date:2025/04/30 Views:108 Category:Python
-
The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e
Pandas DataFrame DataFrame.min() function
Publish Date:2025/04/30 Views:162 Category:Python
-
Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa