2017-07-31 75 views
1

我有一个DataFrame被设置为使得一列国家名称被设置为索引列。我想更改该索引列的标题。这似乎是一件简单的事情,但我无法找到如何去做。如何做呢?这里的食品指数如何变成“国家”?如何更改pandas DataFrame的索引列的名称?

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 

回答

2

试试这个:

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

演示:

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 
+1

'rename_axis'的巨大优势是它会返回一个副本并允许链接。 – piRSquared

2

food是你列索引的名字不是你的索引名。

您可以明确地设置这样的:

df.index.name = 'countries' 

输出:

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 

而且,从列索引名称中删除food

df.columns.name = None 

输出:

    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 
0

熊猫有一个Index.rename() method.是这样工作的:

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 

重命名数据框的索引:

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 

的基本系列,构成了列现在有因为transpose()的名称。所有我们需要做的是重新命名为空字符串:

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 
+0

嗯,这是做一些奇怪的事情:现在我看到“国家”上面写的“食品”,他们都在索引列上方。我期待将“食物”一词改为“国家”。 – BlandCorporation

+0

编辑了使用您的数据的答案,并解决了列转换中列的名称。 –

0

我不喜欢这个了@ MaxU的答案,因为它的速度较慢,但​​更短的代码,不管它的价值。

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 
相关问题