13

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?

user3731622
  • 4,844
  • 8
  • 45
  • 84

1 Answers1

8

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')
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • 4
    Ok, but why do we need to convert each element in index to a `str` when I want to rename a column? Why doesn't `df.rename(columns={"A": "a", "B": "c"})` this work? – user3731622 Feb 07 '17 at 02:07
  • 3
    I believe the docs is trying to show you how to use the *index* parameter, (may not be a clear way though), as `df.rename(columns={"A": "a", "B": "c"})` should work jus fine. – Psidom Feb 07 '17 at 02:09
  • 3
    @user3731622 The way you show `rename()` in your comments does in fact work. You don't need to specify index, it's optional. – the_constant Feb 07 '17 at 02:09
  • 1
    ok, thanks. I must have had another issue b/c I thought I tried it but it didn't work. `df.rename(columns={"A": "a", "B": "c"})` does work. – user3731622 Feb 07 '17 at 17:51