Pandas df.reset_index() function
df.reset_index() is a function in Pandas used to reset the row index of a DataFrame.
During data processing, the index may become discontinuous or not meet your needs.reset_index() can reset the index to the default integer index (0, 1, 2, ...), or convert the current index into a column. This is very useful for tidying data after cleaning, sorting, and filtering.
Basic syntax and parameters
reset_index() is a member function of DataFrame, called via the dot operator . to call it.
Syntax format
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
Parameter Description
| Parameters |
| level |
| drop |
| inplace |
| col_level |
| col_fill |
Return value description
- Returns a new DataFrame (if
inplace=False), orNone(ifinplace=True)。 - returns a DataFrame in which the index has been reset, and the original index (if
drop=False) is retained as a new column.
Example
Let us thoroughly master reset_index() through a series of examples from simple to complex.
Example 1: Basic usage - Resetting the index
The most common usage is to reset the index to the default integer index.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
]
,
'Score'
:
[
85
,
92
,
78
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data (default index):"
)
(
df
)
(
"="
50
)
Sort the data, causing the index to become discontinuous
df_sorted
=
df.
sort_values
(
by
=
'Score'
,
ascending
=
False
)
(
"Sorted data (discontinuous index):"
)
(
df_sorted
)
(
"="
50
)
Reset the index
df_reset
=
df_sorted.
reset_index
(
)
(
"After resetting the index:"
)
(
df_reset
)
Expected output:
原始数据(默认索引):
姓名 成绩
0 张三 85
1 李四 92
2 王五 78
==================================================
排序后的数据(索引不连续):
姓名 成绩
1 李四 92
0 张三 85
2 王五 78
==================================================
After resetting the index:
姓名 成绩 index
0 李四 92 1 # 原来的索引作为新列
1 张三 85 0
2 王五 78 2
Code analysis:
- After sorting, the index becomes 1, 0, 2, no longer the consecutive 0, 1, 2.
- Use
reset_index()After reset, the index becomes 0, 1, 2. - The original index is retained as a new column "index".
Example 2: Using the drop parameter to discard the original index
If you do not need to retain the original index, you can use drop=True parameter was added.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
]
,
'Score'
:
[
85
,
92
,
78
]
}
df
=
pd.
DataFrame
(
data
)
Sort the data
df_sorted
=
df.
sort_values
(
by
=
'Score'
,
ascending
=
False
)
(
"Sorted data:"
)
(
df_sorted
)
(
"="
50
)
Reset the index, discarding the original index
df_reset
=
df_sorted.
reset_index
(
drop
=
True
)
(
"After resetting the index (discarding the original index):"
)
(
df_reset
)
Expected output:
排序后的数据:
姓名 成绩
1 李四 92
0 张三 85
2 王五 78
==================================================
After resetting the index (discarding the original index):
姓名 成绩
0 李四 92
1 张三 85
2 王五 78
Code analysis:
- Use
drop=TrueAfter that, the original index is discarded directly and not retained as a new column. - This is useful when you do not need to retain the original index information.
Example 3: Resetting the index after filtering data
After filtering data, the index may become discontinuous; using reset_index() can tidy the data.
Example
import
pandas
as
pd
import
numpy
as
np
Create a DataFrame containing all students' scores
data
=
{
'Name'
:
[
'Zhang San'
,
'Li Si'
,
'Wang Wu'
,
'Zhao Liu'
,
'Qian Qi'
]
,
'Score'
:
[
85
,
92
,
78
,
90
,
88
]
}
df
=
pd.
DataFrame
(
data
)
(
"Original data:"
)
(
df
)
(
"="
50
)
Filter students with scores greater than 85
df_filtered
=
df
[
df
[
'Score'
]
>
;
85
]
(
"Filtered data (discontinuous index):"
)
(
df_filtered
)
(
"="
50
)
Reset the index
df_reset
=
df_filtered.
reset_index
(
drop
=
True
)
(
"After resetting the index:"
)
(
df_reset
)
Expected output:
原始数据:
姓名 成绩
0 张三 85
1 李四 92
2 王五 78
3 赵六 90
4 钱七 88
==================================================
筛选后的数据(索引不连续):
姓名 成绩
1 李四 92
3 赵六 90
4 钱七 88
Code analysis:
- After filtering, the retained original indices are 1, 3, 4, not the consecutive 0, 1, 2.
- Use
reset_index(drop=True)You can obtain a consecutive index, which is more convenient for subsequent processing.
Common Pandas functions