详谈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 相关文章推荐
Django1.3添加app提示模块不存在的解决方法
Aug 26 Python
使用Python中的线程进行网络编程的入门教程
Apr 15 Python
修复CentOS7升级Python到3.6版本后yum不能正确使用的解决方法
Jan 26 Python
python实现停车管理系统
Nov 30 Python
django的ORM操作 删除和编辑实现详解
Jul 24 Python
Django项目后台不挂断运行的方法
Aug 31 Python
利用python3 的pygame模块实现塔防游戏
Dec 30 Python
tensorflow使用range_input_producer多线程读取数据实例
Jan 20 Python
python3.6使用SMTP协议发送邮件
May 20 Python
Python的logging模块基本用法
Dec 24 Python
Python中全局变量和局部变量的理解与区别
Feb 07 Python
Python中可变和不可变对象的深入讲解
Aug 02 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隐形一句话后门,和ThinkPHP框架加密码程序(base64_decode)
2011/11/02 PHP
PHP连接MySQL查询结果中文显示乱码解决方法
2013/10/25 PHP
PHP实现支持SSL连接的SMTP邮件发送类
2015/03/05 PHP
php源码分析之DZX1.5加密解密函数authcode用法
2015/06/17 PHP
zend framework重定向方法小结
2016/05/28 PHP
浅谈Laravel队列实现原理解决问题记录
2017/08/19 PHP
php设计模式之工厂方法模式分析【星际争霸游戏案例】
2020/01/23 PHP
PHP解密支付宝小程序的加密数据、手机号的示例代码
2021/02/26 PHP
js动态给table添加/删除tr的方法
2013/08/02 Javascript
JavaScript中用于四舍五入的Math.round()方法讲解
2015/06/15 Javascript
Angular.Js的自动化测试详解
2016/12/09 Javascript
Vue CLI2升级至Vue CLI3的方法步骤
2019/05/20 Javascript
Vue移动端项目实现使用手机预览调试操作
2020/07/18 Javascript
[51:36]Optic vs Newbee 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
Python实现批量把SVG格式转成png、pdf格式的代码分享
2014/08/21 Python
Python切片操作实例分析
2018/03/16 Python
python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
2018/09/13 Python
Python 控制终端输出文字的实例
2019/07/12 Python
python zip,lambda,map函数代码实例
2020/04/04 Python
使用Keras中的ImageDataGenerator进行批次读图方式
2020/06/17 Python
全网最细 Python 格式化输出用法讲解(推荐)
2021/01/18 Python
美国CVS药店官网:CVS Pharmacy
2018/07/26 全球购物
eBay瑞士购物网站:eBay.ch
2018/12/24 全球购物
PHP解析URL是哪个函数?怎么用?
2013/05/09 面试题
社区志愿者心得体会
2014/01/03 职场文书
毕业证丢失证明
2014/01/15 职场文书
售后服务经理岗位职责范本
2014/02/22 职场文书
经典英文广告词
2014/03/18 职场文书
最美乡村教师观后感
2015/06/11 职场文书
小学主题班会教案
2015/08/17 职场文书
MySQL数字类型自增的坑
2021/05/07 MySQL
Java常用工具类汇总 附示例代码
2021/06/26 Java/Android
SQL优化老出错,那是你没弄明白MySQL解释计划用法
2021/11/27 MySQL
Python面试不修改数组找出重复的数字
2022/05/20 Python
Ubuntu18.04下QT开发Android无法连接设备问题解决实现
2022/06/01 Java/Android
SpringBoot项目部署到阿里云服务器的实现步骤
2022/06/28 Java/Android