什么是参数“索引"?在Pandas.DataFrame.rename方法中? [英] What is the parameter "index" in Pandas.DataFrame.rename method?

查看:159
本文介绍了什么是参数“索引"?在Pandas.DataFrame.rename方法中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pandas DataFrame具有一个重命名方法,该方法带有一个名为"index"的参数. 我不理解文档中参数的描述: DataFrame.rename

Pandas DataFrame has a rename method which takes a parameter named "index." I don't understand the description of the parameter in the documentation: DataFrame.rename

具体地说,我正在像文档网页上的示例一样使用它:

Specifically, I'm using it like the example on the documentation web page:

df.rename(index=str, columns={"A": "a", "B": "c"})

我了解结果,但不明白为什么设置index=str.

I understand the result, but I don't understand why we set index=str.

index参数用于什么? 为什么该示例设置index=str?

What is the index parameter used for? Why does the example set index=str?

推荐答案

index 参数用于重命名索引,请以 df 为例:

The index parameter is used to rename index, take df from the example:

df.index
# RangeIndex(start=0, stop=3, step=1)

df.rename(index=str).index                               # converts index from int to str
# Index(['0', '1', '2'], dtype='object')

之所以可行,是因为在 rename 函数中,您还可以将函数传递给 index columns 参数,这些参数将应用于索引和列.在这里, str 充当一个函数,并将每个索引从 int 转换为 str 对象.

This works because in the rename function, you can also pass functions to index and columns parameter which will be applied to each element in the index and columns. Here str acts as a function and convert every index from int to str object.

另一个例子:

df.rename(index=lambda x: x*2).index
# Int64Index([0, 2, 4], dtype='int64')

这篇关于什么是参数“索引"?在Pandas.DataFrame.rename方法中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆