Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename an index row in Python Pandas? [duplicate]

Tags:

python

pandas

I see how to rename columns, but I want to rename an index (row name) that I have in a data frame.

I had a table with 350 rows in it, I then added a total to the bottom. I then removed every row except the last row.

-------------------------------------------------
|            |     A      |     B     |     C    |
-------------------------------------------------
|     TOTAL  |    1243    |       423 |     23   |
-------------------------------------------------

So I have the row called 'Total', and then several columns. I want to rename the word 'Total' to something else.

Is this even possible?

Many thanks

like image 363
ScoutEU Avatar asked Apr 26 '17 18:04

ScoutEU


2 Answers

You could use a dictionary structure with rename(), for example,

In [1]: import pandas as pd

        df = pd.Series([1, 2, 3])
        df
Out[1]: 0    1
        1    2
        2    3
        dtype: int64

In [2]: df.rename({1: 3, 2: 'total'})
Out[2]: 0        1
        3        2
        total    3
        dtype: int64
like image 88
mforezdev Avatar answered Nov 14 '22 21:11

mforezdev


Easy as this...

df.index.name = 'Name'
like image 31
SGhaleb Avatar answered Nov 14 '22 23:11

SGhaleb