2017-07-31 106 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 
相關問題