获取和设置 Pandas DataFrame 索引名

Suraj Joshi 2023年1月30日 2021年1月22日
  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

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

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

LinkedIn

相关文章 - Pandas Index