What is the parameter "index" in Pandas.DataFrame.rename method? What is the parameter "index" in Pandas.DataFrame.rename method? pandas pandas

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


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