獲取和設定 Pandas DataFrame 索引名

Suraj Joshi 2023年1月30日
  1. 獲取 DataFrame 中索引列的名稱
  2. 通過設定 name 屬性來設定 DataFrame 的索引列的名稱
  3. 使用 rename_axis() 方法設定 DataFrame 的索引列的名稱
獲取和設定 Pandas DataFrame 索引名

本教程介紹瞭如何在 Pandas DataFrame 中設定和獲取索引列的名稱。我們將在文章中使用下面的 DataFrame 示例。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
        "Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
        "Score": [85, 87, 90, 89],
    },
    index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)

print(my_df)

輸出:

           Applicant  Hometown  Score
2021-01-03     Ratan     Delhi     85
2021-01-04      Anil      Pune     87
2021-01-05    Mukesh  Dhangadi     90
2021-01-06     Kamal   Kolkata     89

獲取 DataFrame 中索引列的名稱

我們可以通過索引列的 name 屬性來獲取 DataFrame 的索引列的名稱。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
        "Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
        "Score": [85, 87, 90, 89],
    },
    index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)

print("The DataFrame is:")
print(my_df, "\n")

print("Name of Index Column of the DataFrame is:")
print(my_df.index.name)

輸出:

The DataFrame is:
           Applicant  Hometown  Score
2021-01-03     Ratan     Delhi     85
2021-01-04      Anil      Pune     87
2021-01-05    Mukesh  Dhangadi     90
2021-01-06     Kamal   Kolkata     89

Name of Index Column of the DataFrame is:
None

由於我們沒有為 my_df DataFrame 設定索引列的名稱,所以得到 my_df DataFrame 的索引列名稱為 None

通過設定 name 屬性來設定 DataFrame 的索引列的名稱

我們只需設定 DataFrame 的 index 屬性的 name 值,就可以設定 DataFrame 的索引列的名稱。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
        "Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
        "Score": [85, 87, 90, 89],
    },
    index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)

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

my_df.index.name = "Date"

print("DataFrame after setting the name of Index Column:")
print(my_df, "\n")

print("Name of Index Column of the DataFrame is:")
print(my_df.index.name)

輸出:

Initial DataFrame:
           Applicant  Hometown  Score
2021-01-03     Ratan     Delhi     85
2021-01-04      Anil      Pune     87
2021-01-05    Mukesh  Dhangadi     90
2021-01-06     Kamal   Kolkata     89

DataFrame after setting the name of Index Column:
           Applicant  Hometown  Score
Date
2021-01-03     Ratan     Delhi     85
2021-01-04      Anil      Pune     87
2021-01-05    Mukesh  Dhangadi     90
2021-01-06     Kamal   Kolkata     89

Name of Index Column of the DataFrame is:
Date

它將 my_dfindex 屬性值設定為 Date

使用 rename_axis() 方法設定 DataFrame 的索引列的名稱

我們可以將索引列的名稱作為引數傳遞給 rename_axis() 方法來設定 DataFrame 中索引列的名稱。

import pandas as pd

my_df = pd.DataFrame(
    {
        "Applicant": ["Ratan", "Anil", "Mukesh", "Kamal"],
        "Hometown": ["Delhi", "Pune", "Dhangadi", "Kolkata"],
        "Score": [85, 87, 90, 89],
    },
    index=["2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06"],
)

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

my_df = my_df.rename_axis("Date")

print("DataFrame after setting the name of Index Column:")
print(my_df, "\n")

print("Name of Index Column of the DataFrame is:")
print(my_df.index.name)

輸出:

Initial DataFrame:
           Applicant  Hometown  Score
2021-01-03     Ratan     Delhi     85
2021-01-04      Anil      Pune     87
2021-01-05    Mukesh  Dhangadi     90
2021-01-06     Kamal   Kolkata     89

DataFrame after setting the name of Index Column:
           Applicant  Hometown  Score
Date
2021-01-03     Ratan     Delhi     85
2021-01-04      Anil      Pune     87
2021-01-05    Mukesh  Dhangadi     90
2021-01-06     Kamal   Kolkata     89

Name of Index Column of the DataFrame is:
Date

它使用 rename_axis() 方法將 DataFrame my_dfindex 列名設定為 Date

作者: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

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

LinkedIn

相關文章 - Pandas Index