Pandas DataFrame 刪除索引

Suraj Joshi 2023年1月30日
  1. 使用 reset_index() 方法刪除 Pandas DataFrame 的索引
  2. 使用 set_index() 方法刪除 Pandas DataFrame 的索引
Pandas DataFrame 刪除索引

本教程將介紹如何刪除 Pandas DataFrame 的索引。

我們將使用下面顯示的 DataFrame 來展示如何刪除索引。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

print(my_df)

輸出:

    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

使用 reset_index() 方法刪除 Pandas DataFrame 的索引

pandas.DataFrame.reset_index() 會將 DataFrame 的索引重置為預設索引。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

df_reset = my_df.reset_index()

print("Before reseting Index:")
print(my_df, "\n")

print("After reseting Index:")
print(df_reset)

輸出:

Before reseting Index:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After reseting Index:
  index   Person      City Mother Tongue  Age
0     A    Alice    Berlin         German   37
1     B   Steven  Montreal         French   20
2     C  Neesham   Toronto        English   38
3     D    Chris      Rome        Italian   23
4     E    Alice    Munich         German   35

它將重置 DataFrame 的索引,但現在的索引將顯示為 index 列。如果我們想刪除 index 列,我們可以在 reset_index() 方法中設定 drop=True

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

df_reset = my_df.reset_index(drop=True)

print("Before reseting Index:")
print(my_df, "\n")

print("After reseting Index:")
print(df_reset)

輸出:

Before reseting Index:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After reseting Index:
    Person      City Mother Tongue  Age
0    Alice    Berlin         German   37
1   Steven  Montreal         French   20
2  Neesham   Toronto        English   38
3    Chris      Rome        Italian   23
4    Alice    Munich         German   35

使用 set_index() 方法刪除 Pandas DataFrame 的索引

pandas.DataFrame.set_index() 將把作為引數傳遞的列設定為 DataFrame 的索引,覆蓋初始索引。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Person": ["Alice", "Steven", "Neesham", "Chris", "Alice"],
        "City": ["Berlin", "Montreal", "Toronto", "Rome", "Munich"],
        "Mother Tongue": ["German", "French", "English", "Italian", "German"],
        "Age": [37, 20, 38, 23, 35],
    },
    index=["A", "B", "C", "D", "E"],
)

df_reset = my_df.set_index("Person")

print("Initial DataFrame:")
print(my_df, "\n")

print("After setting Person column as Index:")
print(df_reset)

輸出:

Initial DataFrame:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After setting Person column as Index:
             City Mother Tongue  Age
Person
Alice      Berlin         German   37
Steven   Montreal         French   20
Neesham   Toronto        English   38
Chris        Rome        Italian   23
Alice      Munich         German   35

它將 Person 列設定為 my_df DataFrame 的索引,覆蓋了 DataFrame 的初始索引。

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

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

LinkedIn

相關文章 - Pandas Index