python 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.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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值