What is the parameter “index” in Pandas.DataFrame.rename method?

江枫思渺然 提交于 2019-12-10 02:58:41

问题


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"})

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

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


回答1:


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')

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.


Another example:

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


来源:https://stackoverflow.com/questions/42080360/what-is-the-parameter-index-in-pandas-dataframe-rename-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!