欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 重命名轴索引的方法

程序员文章站 2023-11-09 19:59:10
如下所示: import numpy as np from pandas import series, dataframe ###重命名轴索引 data =...

如下所示:

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 重命名轴索引的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。