详谈Pandas中iloc和loc以及ix的区别


Posted in Python onJune 08, 2018

Pandas库中有iloc和loc以及ix可以用来索引数据,抽取数据。但是方法一多也容易造成混淆。下面将一一来结合代码说清其中的区别。

1. iloc和loc的区别:

iloc主要使用数字来索引数据,而不能使用字符型的标签来索引数据。而loc则刚好相反,只能使用字符型标签来索引数据,不能使用数字来索引数据,不过有特殊情况,当数据框dataframe的行标签或者列标签为数字,loc就可以来其来索引。

好,先上代码,先上行标签和列标签都为数字的情况。

import pandas as pd
import numpy as np
a = np.arange(12).reshape(3,4)
print a
>>>
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]
df = pd.DataFrame(a)
print df
>>>
 0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
print df.loc[0]
>>>
0 0
1 1
2 2
3 3
Name: 0, dtype: int32
print df.iloc[0]
0 0
1 1
2 2
3 3
Name: 0, dtype: int32
print df.loc[:,[0,3]]
 0 3
0 0 3
1 4 7
2 8 11
print df.iloc[:,[0,3]]
 0 3
0 0 3
1 4 7
2 8 11

接下来是把行标签[0, 1, 2]改成['a', 'b', 'c'],则成这样了。

df.index = ['a','b','c'] 
print df 
>>> 
 0 1 2 3 
a 0 1 2 3 
b 4 5 6 7 
c 8 9 10 11 
print df.loc[0] 
# TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <type 'int'> 
print df.iloc[0] 
>>> 
0 0 
1 1 
2 2 
3 3 
Name: a, dtype: int32 
print df.iloc['a'] # TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [a] of <type 'str'> 
print df.loc['a'] # 正确 
>>> 
0 0 
1 1 
2 2 
3 3 
Name: a, dtype: int32

同样地,把列标签[0, 1, 2, 3]改成['A', 'B, 'C', 'D'],则成这样了。

df.columns = ['A','B','C','D']
print df
>>>
 A B C D
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
print df.loc[:,'A']
>>>
a 0
b 4
c 8
Name: A, dtype: int32
print df.iloc[:,'A'] # ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

2.ix是一种混合索引,字符型标签和整型数据索引都可以。

print df.ix[0]
>>>
A 0
B 1
C 2
D 3
Name: a, dtype: int32
print df.ix['a']
>>>
A 0
B 1
C 2
D 3
Name: a, dtype: int32
print df.ix[:,0]
>>>
a 0
b 4
c 8
Name: A, dtype: int32
print df.ix[:,'A']
>>>
a 0
b 4
c 8
Name: A, dtype: int32

以上这篇详谈Pandas中iloc和loc以及ix的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python的内建模块collections的教程
Apr 28 Python
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 Python
Python用sndhdr模块识别音频格式详解
Jan 11 Python
pytorch训练imagenet分类的方法
Jul 27 Python
python实现基于信息增益的决策树归纳
Dec 18 Python
Python计算库numpy进行方差/标准方差/样本标准方差/协方差的计算
Dec 28 Python
PyQt5根据控件Id获取控件对象的方法
Jun 25 Python
python使用配置文件过程详解
Dec 28 Python
基于K.image_data_format() == 'channels_first' 的理解
Jun 29 Python
pycharm-professional-2020.1下载与激活的教程
Sep 21 Python
详解pandas中利用DataFrame对象的.loc[]、.iloc[]方法抽取数据
Dec 13 Python
Django数据库(SQlite)基本入门使用教程
Jul 07 Python
python实现人人自动回复、抢沙发功能
Jun 08 #Python
利用Python写一个爬妹子的爬虫
Jun 08 #Python
python os用法总结
Jun 08 #Python
Python DataFrame 设置输出不显示index(索引)值的方法
Jun 07 #Python
浅谈Pandas 排序之后索引的问题
Jun 07 #Python
pandas.dataframe中根据条件获取元素所在的位置方法(索引)
Jun 07 #Python
python pandas 对series和dataframe的重置索引reindex方法
Jun 07 #Python
You might like
php数组键名技巧小结
2015/02/17 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
javascript 特性检测并非浏览器检测
2010/01/15 Javascript
javascript简易缓动插件(源码打包)
2012/02/16 Javascript
JS判断文本框内容改变事件的简单实例
2014/03/07 Javascript
JavaScript onkeypress事件入门实例(按下或按住一个键盘按键)
2014/10/17 Javascript
比例尺、缩略图、平移缩放之百度地图添加控件方法
2015/08/03 Javascript
基于insertBefore制作简单的循环插空效果
2015/09/21 Javascript
JS和canvas实现俄罗斯方块
2017/03/14 Javascript
Vue实现带进度条的文件拖动上传功能
2018/02/23 Javascript
详解nuxt sass全局变量(公共scss解决方案)
2018/06/27 Javascript
Vue条件循环判断+计算属性+绑定样式v-bind的实例
2018/09/18 Javascript
小程序根据手机机型设置自定义底部导航距离
2019/06/04 Javascript
javascript设计模式 ? 工厂模式原理与应用实例分析
2020/04/09 Javascript
openlayers实现地图测距测面
2020/09/25 Javascript
Python中str.format()详解
2017/03/12 Python
Python实现的插入排序算法原理与用法实例分析
2017/11/22 Python
Python语言实现百度语音识别API的使用实例
2017/12/13 Python
python 美化输出信息的实例
2018/10/15 Python
python 获得任意路径下的文件及其根目录的方法
2019/02/16 Python
Python datetime和unix时间戳之间相互转换的讲解
2019/04/01 Python
基于python监控程序是否关闭
2020/01/14 Python
django 装饰器 检测登录状态操作
2020/07/02 Python
在PyCharm中安装PaddlePaddle的方法
2021/02/05 Python
HTML5中input[type='date']自定义样式与日历校验功能的实现代码
2017/07/11 HTML / CSS
英国婴儿产品专家:Samuel Johnston
2020/04/20 全球购物
项目资料员岗位职责
2013/12/10 职场文书
教师党员岗位承诺书
2014/05/29 职场文书
离婚起诉书范文2015
2015/05/19 职场文书
大学生安全教育主题班会
2015/08/12 职场文书
幼儿园六一儿童节开幕词
2016/03/04 职场文书
《弟子规》读后感:知廉耻、明是非、懂荣辱、辨善恶
2019/12/03 职场文书
提升Nginx性能的一些建议
2021/03/31 Servers
Go语言基础map用法及示例详解
2021/11/17 Golang
JavaWeb实现显示mysql数据库数据
2022/03/19 Java/Android
Python OpenCV超详细讲解读取图像视频和网络摄像头
2022/04/02 Python