Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas df.reset_index() function

Pandas df.reset_index() function

Pandas Common Functions Common Pandas functions


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


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

)

print

(

"Original data (default index):"

)

print

(

df

)

print

(

"="

50

)

Sort the data, causing the index to become discontinuous

df_sorted

=

df.

sort_values

(

by

=

'Score'

,

ascending

=

False

)

print

(

"Sorted data (discontinuous index):"

)

print

(

df_sorted

)

print

(

"="

50

)

Reset the index

df_reset

=

df_sorted.

reset_index

(

)

print

(

"After resetting the index:"

)

print

(

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:

  1. After sorting, the index becomes 1, 0, 2, no longer the consecutive 0, 1, 2.
  2. Use reset_index() After reset, the index becomes 0, 1, 2.
  3. 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

)

print

(

"Sorted data:"

)

print

(

df_sorted

)

print

(

"="

50

)

Reset the index, discarding the original index

df_reset

=

df_sorted.

reset_index

(

drop

=

True

)

print

(

"After resetting the index (discarding the original index):"

)

print

(

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:

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

)

print

(

"Original data:"

)

print

(

df

)

print

(

"="

50

)

Filter students with scores greater than 85

df_filtered

=

df

[

df

[

'Score'

]

>

;

85

]

print

(

"Filtered data (discontinuous index):"

)

print

(

df_filtered

)

print

(

"="

50

)

Reset the index

df_reset

=

df_filtered.

reset_index

(

drop

=

True

)

print

(

"After resetting the index:"

)

print

(

df_reset

)

Expected output:

原始数据:

   姓名  成绩

0  张三   85

1  李四   92

2  王五   78

3  赵六   90

4  钱七   88

==================================================

筛选后的数据(索引不连续):

   姓名  成绩

1  李四   92

3  赵六   90

4  钱七   88

Code analysis: