获取和设置 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_df
的 index
属性值设置为 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_df
的 index
列名设置为 date
。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
pandas dataframe dataframe.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:python
-
dataframe.shift() 函数是将 dataframe 的索引按指定的周期数进行移位。
python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:python
-
python pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:python
-
pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 dataframe 中。
pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:python
-
本教程介绍了如何在 pandas 中使用 dataframe.merge()方法合并两个 dataframes。
pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:python
-
本教程介绍了如何使用 python 中的 loc 和 iloc 从 pandas dataframe 中过滤数据。
在 python 中将 pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:python
-
了解如何在 python 中将 pandas 系列日期时间转换为字符串