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入门篇之条件、循环
Oct 17 Python
Python常见格式化字符串方法小结【百分号与format方法】
Sep 18 Python
Python网络编程使用select实现socket全双工异步通信功能示例
Apr 09 Python
python实现对图片进行旋转,放缩,裁剪的功能
Aug 07 Python
详解Python绘图Turtle库
Oct 12 Python
python opencv根据颜色进行目标检测的方法示例
Jan 15 Python
使用Python 自动生成 Word 文档的教程
Feb 13 Python
Matplotlib.pyplot 三维绘图的实现示例
Jul 28 Python
Python 下载Bing壁纸的示例
Sep 29 Python
Scrapy实现模拟登录的示例代码
Feb 21 Python
Python requests库参数提交的注意事项总结
Mar 29 Python
python游戏开发Pygame框架
Apr 22 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数字格式化
2006/12/06 PHP
PHP取整数函数常用的四种方法小结
2012/07/05 PHP
浅析Mysql 数据回滚错误的解决方法
2013/08/05 PHP
PHP解析目录路径的3个函数总结
2014/11/18 PHP
PHP Imagick完美实现图片裁切、生成缩略图、添加水印
2016/02/22 PHP
PHP获取类私有属性的3种方法
2020/09/10 PHP
网上抓的一个特效
2007/05/11 Javascript
JQuery Tips(2) 关于$()包装集你不知道的
2009/12/14 Javascript
弹出层之1:JQuery.Boxy (一) 使用介绍
2011/10/06 Javascript
web网页按比例显示图片实现原理及js代码
2013/08/09 Javascript
jquery cookie实现的简单换肤功能适合小网站
2013/08/25 Javascript
jquery div拖动效果示例代码
2013/12/08 Javascript
jQuery选择器源码解读(五):tokenize的解析过程
2015/03/31 Javascript
基于javascript实现九九乘法表
2016/03/27 Javascript
canvas绘制多边形
2017/02/24 Javascript
ES6新增的math,Number方法
2017/08/06 Javascript
简单的vuex 的使用案例笔记
2018/04/13 Javascript
最适应的vue.js的form提交涉及多种插件【推荐】
2018/08/27 Javascript
js实现简单的秒表
2020/01/16 Javascript
js 函数性能比较方法
2020/08/24 Javascript
[01:20]DOTA2更新全新英雄 天涯墨客现已加入游戏
2018/08/25 DOTA
Python写入数据到MP3文件中的方法
2015/07/10 Python
python调用staf自动化框架的方法
2018/12/26 Python
python原类、类的创建过程与方法详解
2019/07/19 Python
python 调试冷知识(小结)
2019/11/11 Python
windows环境中利用celery实现简单任务队列过程解析
2019/11/29 Python
PyQt5 文本输入框自动补全QLineEdit的实现示例
2020/05/13 Python
tensorflow使用CNN分析mnist手写体数字数据集
2020/06/17 Python
英国网上香水店:Fragrance Direct
2016/07/20 全球购物
2013年研究生毕业感言
2014/02/06 职场文书
喷漆工的岗位职责
2014/03/17 职场文书
社区工作者演讲稿
2014/05/23 职场文书
初中生思想道德自我评价
2015/03/09 职场文书
廉政党课工作报告案例
2019/06/21 职场文书
2019通用版新员工入职培训方案!
2019/07/11 职场文书
Android开发手册TextInputLayout样式使用示例
2022/06/10 Java/Android