Pandas DataFrame 重置索引

Suraj Joshi 2023年1月30日
  1. Pandas DataFrame reset_index() 方法
  2. 使用 pandas.DataFrame.reset_index() 方法重置 DataFrame 的索引
Pandas DataFrame 重置索引

本教程介绍了如何使用 pandas.DataFrame.reset_index() 来重置 Pandas DataFrame 中的索引。reset_index() 方法将 DataFrame 的索引设置为默认索引,数字范围从 0(DataFrame 中的行数-1)

Pandas DataFrame reset_index() 方法

语法

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill="")

使用 pandas.DataFrame.reset_index() 方法重置 DataFrame 的索引

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    },
    index=roll_no,
)

print(student_df)

输出:

        Name  Age      City Grade
501    Alice   17  New York     A
502   Steven   20  Portland    B-
503  Neesham   18    Boston    B+
504    Chris   21   Seattle    A-
505    Alice   15    Austin     A

假设我们有一个 DataFrame,有 5 行 4 列,如输出所示。我们在 DataFrame 中还设置了一个索引。

重置 DataFrame 的索引,保持 DataFrame 的初始索引为列

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    },
    index=roll_no,
)

print("Initial DataFrame:")
print(student_df)
print("")

print("DataFrame after reset_index:")
student_df.reset_index(inplace=True, drop=False)
print(student_df)

输出:

Initial DataFrame:
        Name  Age      City Grade
501    Alice   17  New York     A
502   Steven   20  Portland    B-
503  Neesham   18    Boston    B+
504    Chris   21   Seattle    A-
505    Alice   15    Austin     A

DataFrame after reset_index:
   index     Name  Age      City Grade
0    501    Alice   17  New York     A
1    502   Steven   20  Portland    B-
2    503  Neesham   18    Boston    B+
3    504    Chris   21   Seattle    A-
4    505    Alice   15    Austin     A

它将 DataFrame student_df 的索引重置为默认索引。inplace=True 会在原 DataFrame 本身进行更改,如果我们使用 drop=False,初始索引会被放置在 DataFrame 中作为列。如果我们使用 drop=False,在使用 reset_index() 方法后,初始索引会被放置在 DataFrame 中作为一列。

重置 DataFrame 的索引,删除 DataFrame 的初始索引

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    },
    index=roll_no,
)

print("Initial DataFrame:")
print(student_df)
print("")

print("DataFrame after reset_index:")
student_df.reset_index(inplace=True, drop=True)
print(student_df)

输出:

Initial DataFrame:
        Name  Age      City Grade
501    Alice   17  New York     A
502   Steven   20  Portland    B-
503  Neesham   18    Boston    B+
504    Chris   21   Seattle    A-
505    Alice   15    Austin     A

DataFrame after reset_index:
      Name  Age      City Grade
0    Alice   17  New York     A
1   Steven   20  Portland    B-
2  Neesham   18    Boston    B+
3    Chris   21   Seattle    A-
4    Alice   15    Austin     A

它将 DataFrame student_df 的索引重置为默认索引。由于我们在 reset_index() 方法中设置了 drop=True,初始索引从 DataFrame 中被删除。

删除行后重置 DataFrame 的索引

import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
    {
        "Name": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "Age": [17, 20, 18, 21, 15],
        "City": ["New York", "Portland", "Boston", "Seattle", "Austin"],
        "Grade": ["A", "B-", "B+", "A-", "A"],
    }
)

student_df.drop([2, 3], inplace=True)
print("Initial DataFrame:")
print(student_df)
print("")

student_df.reset_index(inplace=True, drop=True)
print("DataFrame after reset_index:")
print(student_df)

输出:

Initial DataFrame:
     Name  Age      City Grade
0   Alice   17  New York     A
1  Steven   20  Portland    B-
4   Alice   15    Austin     A

DataFrame after reset_index:
     Name  Age      City Grade
0   Alice   17  New York     A
1  Steven   20  Portland    B-
2   Alice   15    Austin     A

正如我们在输出中所看到的,我们在删除行后有缺失的索引。在这种情况下,我们可以使用 reset_index() 方法来使用没有缺失值的索引。

如果我们希望将初始索引作为 DataFrame 的列,我们可以在 reset_index() 方法中使用 drop=False

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

相关文章 - Pandas Index