pandas DataFrame索引行列的实现


Posted in Python onJune 04, 2019
  • python版本: 3.6
  • pandas版本: 0.23.4

行索引

索引行有三种方法,分别是 loc iloc ix

import pandas as pd
import numpy as np

index = ["a", "b", "c", "d"]
data = np.random.randint(10, size=(4, 3))
df = pd.DataFrame(data, index=index)

"""
  0 1 2
a 9 7 1
b 0 0 7
c 2 6 5
d 8 2 5
"""

loc

loc通过行索引名字来确定行的

单行索引, 返回Series对象

df.loc["a"]
"""
0  9
1  7
2  1
Name: a, dtype: int64
"""

df.loc["b"]
"""
0  0
1  0
2  7
Name: b, dtype: int64
"""

多行索引, 返回DataFrame对象

df.loc[["a", "c"]]
"""
  0 1 2
a 9 7 1
c 2 6 5
"""

iloc

通过行索引序号来确定行的

单行索引, 返回Series对象

df.iloc[0]
"""
0  9
1  7
2  1
Name: a, dtype: int64
"""

df.iloc[1]
"""
0  0
1  0
2  7
Name: b, dtype: int64
"""

多行索引, 返回DataFrame对象

df.iloc[[0, 2]]
"""
  0 1 2
a 9 7 1
c 2 6 5
"""

ix(不建议使用)

通过行索引名字或序号来确定行的, 如果行索引 index 的类型为整型时, 使用 ix 方法索引时为按行索引名字进行索引, 如行索引名不存在则会报错

index = [2, 3, 4, 5]
df = pd.DataFrame(data, index=index)

"""
  0 1 2
2 9 7 1
3 0 0 7
4 2 6 5
5 8 2 5
"""

df.ix[2]
"""
0  9
1  7
2  1
Name: 2, dtype: int64
"""
# 提示信息
"""
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
"""

# 如果 index 为整数, 则不能按行索引号进行索引
df.ix[0]
"""
...
KeyError: 0
"""

列索引

索引行有两种方法,分别是 . []

import pandas as pd
import numpy as np

columns = ["i", "ii", "iii"]
data = np.random.randint(10, size=(4, 3))
df = pd.DataFrame(data, columns=columns)

"""
  i ii iii 
0 4  5  9 
1 0  3  4 
2 7  9  1 
3 8  2  3 
"""

通过 . 属性直接获取指定行, 返回Series对象

df.i
"""
0  4
1  0
2  7
3  8
Name: i, dtype: int64
"""

 []

单列索引, 返回DataFrame对象

df[["i"]]
"""
  i
0 4
1 0
2 7
3 8
"""

多列索引, 返回DataFrame对象

df[["i", "ii"]]
"""
  i ii
0 4  5
1 0  3
2 7  9
3 8  2
"""

同时索引行及列

通过指定索引名或切片方式进行索引

index = ["a", "f", "c", "h"]
columns = ["i", "ii", "iii"]

df = pd.DataFrame(data, index=index, columns=columns)
"""
  i ii iii
a 4  5  9
f 0  3  4
c 7  9  1
h 8  2  3
"""

loc

通过指定行及列索引名进行索引, 返回DataFrame对象

df.loc[["a", "f"], ["ii", "iii"]]
"""
  ii iii
a  5  9
f  3  4
"""

通过指定行及列索引名范围进行索引(包含边值), 返回DataFrame对象

df.loc["a":"c", "ii":"iii"]
"""
  ii iii
a  5  9
f  3  4
c  9  1
"""

iloc

通过指定行及列索引号进行索引, 返回DataFrame对象

df.iloc[[0, 1], [1, 2]]
"""
  ii iii
a  5  9
f  3  4
"""

通过指定行及列索引号范围进行切片索引(左闭右开), 返回DataFrame对象

df.iloc[:3, 1:3]
"""
  ii iii
a  5  9
f  3  4
c  9  1
"""

ix(不建议使用)

通过指定行及列索引号范围或名字范围进行切片, 返回DataFrame对象

df.ix["a":"c", "i":"iii"]
df.ix["a":"c", 1:3]
df.ix[:3, 1:3]

tips: 只有使用 iloc 或 ix 按索引号进行切片索引时才为左闭右开, 其余全闭

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python网站验证码识别
Jan 25 Python
Python 和 JS 有哪些相同之处
Nov 23 Python
Python将多个excel文件合并为一个文件
Jan 03 Python
python实现Decorator模式实例代码
Feb 09 Python
Python连接Mssql基础教程之Python库pymssql
Sep 16 Python
Python中正反斜杠(‘/’和‘\’)的意义与用法
Aug 12 Python
python实现批量修改文件名
Mar 23 Python
Pytorch转keras的有效方法,以FlowNet为例讲解
May 26 Python
QML用PathView实现轮播图
Jun 03 Python
一篇文章搞懂python的转义字符及用法
Sep 03 Python
python飞机大战游戏实例讲解
Dec 04 Python
python中使用np.delete()的实例方法
Feb 01 Python
深入浅析Python中的迭代器
Jun 04 #Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
Jun 04 #Python
Python利用sqlacodegen自动生成ORM实体类示例
Jun 04 #Python
Python批量生成幻影坦克图片实例代码
Jun 04 #Python
python和mysql交互操作实例详解【基于pymysql库】
Jun 04 #Python
Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例
Jun 04 #Python
Python使用MyQR制作专属动态彩色二维码功能
Jun 04 #Python
You might like
PHP判断图片格式的七种方法小结
2013/06/03 PHP
php教程之魔术方法的使用示例(php魔术函数)
2014/02/12 PHP
Zend Framework框架Smarty扩展实现方法
2016/03/22 PHP
PHP创建单例后台进程的方法示例
2017/05/23 PHP
PHP实现的防止跨站和xss攻击代码【来自阿里云】
2018/01/29 PHP
网页中CDATA标记的说明
2010/09/12 Javascript
拥Bootstrap入怀——导航栏篇
2016/05/30 Javascript
简单模拟node.js中require的加载机制
2016/10/27 Javascript
javascript垃圾收集机制的原理分析
2016/12/08 Javascript
用javascript获取任意颜色的更亮或更暗颜色值示例代码
2017/07/21 Javascript
详解基于mpvue微信小程序下载远程图片到本地解决思路
2019/05/16 Javascript
如何优雅地在Node应用中进行错误异常处理
2019/11/25 Javascript
解决vue 使用setTimeout,离开当前路由setTimeout未销毁的问题
2020/07/21 Javascript
selenium 反爬虫之跳过淘宝滑块验证功能的实现代码
2020/08/27 Javascript
基于Vant UI框架实现时间段选择器
2020/12/24 Javascript
js属性对象的hasOwnProperty方法的使用
2021/02/05 Javascript
Python实现快速排序和插入排序算法及自定义排序的示例
2016/02/16 Python
Python标准库shutil用法实例详解
2018/08/13 Python
django进阶之cookie和session的使用示例
2018/08/17 Python
基于python3监控服务器状态进行邮件报警
2019/10/19 Python
django-crontab实现服务端的定时任务的示例代码
2020/02/17 Python
HTML5 canvas基本绘图之绘制矩形
2016/06/27 HTML / CSS
萌新HTML5 入门指南(二)
2020/11/09 HTML / CSS
美国家庭鞋店:Shoe Sensation
2019/09/27 全球购物
幼儿园园长岗位职责
2013/11/26 职场文书
两年的个人工作自我评价
2014/01/10 职场文书
教师党员承诺书
2014/03/25 职场文书
心理健康日活动总结
2014/05/08 职场文书
十一国庆节“向国旗敬礼”主题班会活动方案
2014/09/27 职场文书
同学聚会邀请函
2015/01/30 职场文书
大学生心理健康活动总结
2015/05/08 职场文书
小学生教师节广播稿
2015/08/19 职场文书
公文写作:新员工转正申请书范本3篇!
2019/08/07 职场文书
作文之亲情600字
2019/09/23 职场文书
Golang全局变量加锁的问题解决
2021/05/08 Golang
Vue图片裁剪组件实例代码
2021/07/02 Vue.js