Reset Index of DataFrame
To reset the index of a DataFrame to a default one in Pandas, call reset_index() method on this DataFrame.
In this tutorial, we will learn the syntax of DataFrame.reset_index() method and how to use this method to reset the index of this DataFrame to default index.
Syntax
The syntax of pandas DataFrame.reset_index() method is
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
where
Parameter | Value | Description |
---|---|---|
level | int, str, tuple, or list. default value is None. | Only remove the given levels from the index. Removes all levels by default. |
drop | bool. default value is False. | Do not try to insert index into dataframe columns. This resets the index to the default integer index. |
inplace | bool. default value is False. | Modify the DataFrame in place (do not create a new object). |
col_level | int or str. default value is 0. | If the columns have multiple levels, determines which level the labels are inserted into. By default it is inserted into the first level. |
col_fill | object. default value is ‘’. | If the columns have multiple levels, determines how the other levels are named. If None then the index name is repeated. |
Return Value
- DataFrame or
- None if inplace=True.
Examples
Reset Index of DataFrame
In the following program, we take a DataFrame with some specific index, and then set the index to default index using reset_index().
Example.py
import pandas as pd
data = {'col_1': [10, 15, 20, 25], 'col_2': [50, 55, 60, 65]}
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd'])
result = df.reset_index()
print('Original DataFrame')
print(df)
print('\nDataFrame with Index Reset')
print(result)
Output
Original DataFrame
col_1 col_2
a 10 50
b 15 55
c 20 60
d 25 65
DataFrame with Index Reset
index col_1 col_2
0 a 10 50
1 b 15 55
2 c 20 60
3 d 25 65
Avoid adding Index as Column to DataFrame
When we reset the index, the old index is added as a column to the resulting DataFrame. We can avoid this happening by passing True for the parameter drop
.
Example.py
import pandas as pd
data = {'col_1': [10, 15, 20, 25], 'col_2': [50, 55, 60, 65]}
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd'])
result = df.reset_index(drop=True)
print('Original DataFrame')
print(df)
print('\nDataFrame with Index Reset')
print(result)
Output
Original DataFrame
col_1 col_2
a 10 50
b 15 55
c 20 60
d 25 65
DataFrame with Index Reset
col_1 col_2
0 10 50
1 15 55
2 20 60
3 25 65
Reset Index of DataFrame In-place
reset_index() method by default does not modify the original DataFrame. We can tell reset_index() method to modify the DataFrame in-place and not return anything, by passing True to the parameter inplace
.
Example.py
import pandas as pd
data = {'col_1': [10, 15, 20, 25], 'col_2': [50, 55, 60, 65]}
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd'])
df.reset_index(inplace=True)
print(df)
Output
index col_1 col_2
0 a 10 50
1 b 15 55
2 c 20 60
3 d 25 65
Conclusion
In this Pandas Tutorial, we learned the syntax of reset_index() method and how to reset the index of given DataFrame to a default index using this pandas DataFrame.reset index() method.