获取和设置 pandas dataframe 索引名-ag捕鱼王app官网

获取和设置 pandas dataframe 索引名

作者:迹忆客 最近更新:2024/04/24 浏览次数:

本教程介绍了如何在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:python

pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。

pandas 追加数据到 csv 中

发布时间:2024/04/24 浏览次数:352 分类:python

本教程演示了如何在追加模式下使用 to_csv()向现有的 csv 文件添加数据。

pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:python

本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。

pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:python

本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图