python 重命名轴索引的方法

yipeiwu_com7年前Python基础

如下所示:

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设计】。

相关文章

详解Python各大聊天系统的屏蔽脏话功能原理

详解Python各大聊天系统的屏蔽脏话功能原理

突然想到一个视频里面弹幕被和谐的一满屏的*号觉得很有趣,然后就想用python来试试写写看,结果还真玩出了点效果,思路是首先你得有一个脏话存放的仓库好到时候检测,那么个人还是喜欢用列表,...

python3.4用函数操作mysql5.7数据库

本文实例为大家分享了python3.4函数操作mysql数据库的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # -*- coding:utf-8...

修改python plot折线图的坐标轴刻度方法

修改python plot折线图的坐标轴刻度方法

修改python plot折线图的坐标轴刻度,这里修改为整数: 代码如下: from matplotlib import pyplot as plt import matplotl...

完美解决Pycharm无法导入包的问题 Unresolved reference

完美解决Pycharm无法导入包的问题 Unresolved reference

如下所示: Unresolved reference 'ERROR_CODE_INPUT_ERROR' less... (Ctrl+F1) This inspection dete...

Python 编码处理-str与Unicode的区别

一篇关于STR和UNICODE的好文章 整理下python编码相关的内容 注意: 以下讨论为Python2.x版本, Py3k的待尝试 开始 用python处理中文时,读取文件或消息,h...