圆月山庄资源网 Design By www.vgjia.com

如下所示:

import numpy as np
from pandas import Series, DataFrame
###重命名轴索引
data = DataFrame(np.arange(12).reshape((3, 4)),
     index=['Ohio', 'Colorado', 'New York'],
     columns=['one', 'two', 'three', 'four'])
 
print( data.index.map(str.upper) ) #['OHIO' 'COLORADO' 'NEW YORK']
 
data.index = data.index.map(str.upper)
print( data )
'''
   one two three four
OHIO  0 1  2  3
COLORADO 4 5  6  7
NEW YORK 8 9  10 11
'''
 
data_2 = data.rename(index=str.title, columns=str.upper)
print( data_2 )
'''
   ONE TWO THREE FOUR
Ohio  0 1  2  3
Colorado 4 5  6  7
New York 8 9  10 11
'''
data_3 = data.rename(index={'OHIO': 'INDIANA'},
   columns={'three': 'peekaboo'})
print( data_3 )
'''
   one two peekaboo four
INDIANA  0 1   2  3
COLORADO 4 5   6  7
NEW YORK 8 9  10 11
'''
 
# 总是返回DataFrame的引用
_ = data.rename(index={'OHIO': 'INDIANA'}, inplace=True)
print( _ ) # None

以上这篇python 重命名轴索引的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,重命名,轴索引

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com