1

I've got a DataFrame that gets set up such that a column of country names is set as the index column. I want to change the title of that index column. This seems like a simple thing to do, but I can't find how to actually do it. How can it be done? How can the index "foods" column here be changed to "countries"?

import pandas as pd

df = pd.DataFrame(
    [
        ["alcoholic drinks"  ,  375,  135,  458,  475],
        ["beverages"         ,   57,   47,   53,   73],
        ["carcase meat"      ,  245,  267,  242,  227],
        ["cereals"           , 1472, 1494, 1462, 1582],
        ["cheese"            ,  105,   66,  103,  103],
        ["confectionery"     ,   54,   41,   62,   64],
        ["fats and oils"     ,  193,  209,  184,  235],
        ["fish"              ,  147,   93,  122,  160],
        ["fresh fruit"       , 1102,  674,  957, 1137],
        ["fresh potatoes"    ,  720, 1033,  566,  874],
        ["fresh Veg"         ,  253,  143,  171,  265],
        ["other meat"        ,  685,  586,  750,  803],
        ["other veg."        ,  488,  355,  418,  570],
        ["processed potatoes",  198,  187,  220,  203],
        ["processed veg."    ,  360,  334,  337,  365],
        ["soft drinks"       , 1374, 1506, 1572, 1256],
        ["sugars"            ,  156,  139,  147,  175]
    ],
    columns = [
        "foods",
        "England",
        "Northern Ireland",
        "Scotland",
        "Wales"
    ]
)

df = df.set_index("foods")

df = df.transpose()
df = df.rename({"foods": "countries"})
df

4 Answers 4

2

Try this:

df = df.rename_axis("countries", axis=0).rename_axis(None, axis=1)

Demo:

In [10]: df
Out[10]:
                  alcoholic drinks  beverages  carcase meat   ...
countries
England                        375         57           245
Northern Ireland               135         47           267
Scotland                       458         53           242
Wales                          475         73           227
Sign up to request clarification or add additional context in comments.

1 Comment

The great advantage of rename_axis is that it returns a copy and allows for chaining.
2

food is your column index name not your index name.

You can set it explicitly like this:

df.index.name = 'countries'

Output:

foods             alcoholic drinks  beverages  carcase meat  cereals  cheese  \
countries                                                                      
England                        375         57           245     1472     105   
Northern Ireland               135         47           267     1494      66   
Scotland                       458         53           242     1462     103   
Wales                          475         73           227     1582     103  

And, to remove food from column index name:

df.columns.name = None

Output:

                  alcoholic drinks  beverages  carcase meat  cereals  cheese  \
countries                                                                      
England                        375         57           245     1472     105   
Northern Ireland               135         47           267     1494      66   
Scotland                       458         53           242     1462     103   
Wales                          475         73           227     1582     103  

Comments

0

Pandas has an Index.rename() method. Works like this:

import pandas as pd

df = pd.DataFrame(
    [
        ["alcoholic drinks", 375, 135, 458, 475],
        ["beverages", 57, 47, 53, 73],
        ["carcase meat", 245, 267, 242, 227],
        ["cereals", 1472, 1494, 1462, 1582],
        ["cheese", 105, 66, 103, 103],
        ["confectionery", 54, 41, 62, 64],
        ["fats and oils", 193, 209, 184, 235],
        ["fish", 147, 93, 122, 160],
        ["fresh fruit", 1102, 674, 957, 1137],
        ["fresh potatoes", 720, 1033, 566, 874],
        ["fresh Veg", 253, 143, 171, 265],
        ["other meat", 685, 586, 750, 803],
        ["other veg.", 488, 355, 418, 570],
        ["processed potatoes", 198, 187, 220, 203],
        ["processed veg.", 360, 334, 337, 365],
        ["soft drinks", 1374, 1506, 1572, 1256],
        ["sugars", 156, 139, 147, 175]
    ],
    columns=[
        "foods",
        "England",
        "Northern Ireland",
        "Scotland",
        "Wales"
    ]
)

df.set_index('foods', inplace=True)
df = df.transpose()

print(df.head())

foods             confectionery  fats and oils  fish  fresh fruit ...
England                      54            193   147         1102
Northern Ireland             41            209    93          674
Scotland                     62            184   122          957
Wales                        64            235   160         1137

Renaming the index of the DataFrame:

df.index.rename('Countries', inplace=True)
print(df.head())

foods             confectionery  fats and oils  fish  fresh fruit ...
    Countries
England                      54            193   147         1102
Northern Ireland             41            209    93          674
Scotland                     62            184   122          957
Wales                        64            235   160         1137

The underlying Series that makes up the columns now has a name because of the transpose(). All we need to do is rename that to an empty string:

df.columns.rename('', inplace=True)
print(df.head())

                  confectionery  fats and oils  fish  fresh fruit ...
    Countries
England                      54            193   147         1102
Northern Ireland             41            209    93          674
Scotland                     62            184   122          957
Wales                        64            235   160         1137

2 Comments

Hmm, this is doing something weird: now I see "foods" written above "countries" and both of them are above the index column. I am looking to change the word "foods" to "countries".
Edited the answer to use your data and to address the name that the columns Series has from the transpose.
0

I don't prefer this over @MaxU's answer because it's slower, but it is shorter code for whatever that's worth.

df.stack().rename_axis(['countries', None]).unstack()

                  alcoholic drinks  beverages  carcase meat  cereals
countries                                                           
England                        375         57           245     1472
Northern Ireland               135         47           267     1494
Scotland                       458         53           242     1462
Wales                          475         73           227     1582

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.