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.
Key Points –
reset_index()
is used to reset the index of a DataFrame, converting it back to a default integer index.- By default,
reset_index()
moves the existing index into a new column in the DataFrame. - Use
drop=True
to reset the index without adding the old index as a new column. - The
inplace=True
parameter allows you to modify the DataFrame directly without creating a copy. reset_index()
can be combined with therename()
function to adjust column names after reset.- After resetting the index, the DataFrame index is restored to the default range from 0 to n-1.
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 Nonedrop
– bool, default False. When set to True, ignores setting old index as column.inplace
– bool, 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
# 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.
# 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
# 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.
# 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
# Output:
Courses Fee Duration
1 Spark 20000 35days
2 PySpark 20000 35days
3 PHP 10000 30days
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)
FAQ on pandas reset_index() – Rest Index on DataFrame
reset_index()
resets the index of a DataFrame to the default integer-based index (0, 1, 2, …). It also moves the current index to a column, unless specified otherwise.
You can reset the index by calling the reset_index()
method on a DataFrame:
You can reset the index without adding it as a column by using the drop=True
parameter in the reset_index()
method.
By default, reset_index()
returns a new DataFrame and does not modify the original DataFrame. To modify the original DataFrame, you can use inplace=True
.
reset_index()
does not affect the column names, but if you reset the index and add it as a column, the column will be named “index” by default unless you specify a different name.
If the DataFrame already has a default integer index (0, 1, 2,…), calling reset_index()
will add a new column with the existing index values and reset the index back to the default.
Conclusion
In this article, you have learned how to reset the index on DataFrame by using different prams on the reset_index()
method.
Related Articles
- Pandas Drop Index Column Explained
- pandas.DataFrame.sort_values() – Examples
- pandas DataFrame.sort_index() – Sort by Index
- Pandas set_index() – Set Index to DataFrame
- Pandas melt() DataFrame Example
- How to Transpose() DataFrame in Pandas?
- Pandas Drop Rows with NaN Values in DataFrame
- Series.reindex() – Change the Index Order in Pandas Series