详谈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算法学习之基数排序实例
Dec 18 Python
python正则表达式re模块详细介绍
May 29 Python
探索Python3.4中新引入的asyncio模块
Apr 08 Python
使用Python的urllib和urllib2模块制作爬虫的实例教程
Jan 20 Python
python 性能提升的几种方法
Jul 15 Python
Python自动化之数据驱动让你的脚本简洁10倍【推荐】
Jun 04 Python
wxPython实现带颜色的进度条
Nov 19 Python
Pytorch之finetune使用详解
Jan 18 Python
django 外键创建注意事项说明
May 20 Python
利用python控制Autocad:pyautocad方式
Jun 01 Python
如何使用 Python 读取文件和照片的创建日期
Sep 05 Python
Pytest中conftest.py的用法
Jun 27 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的dl函数用法实例
2014/11/06 PHP
PHP版微信小店接口开发实例
2016/11/12 PHP
phpStudy配置多站点多域名和多端口的方法
2017/09/01 PHP
nicejforms——美化表单不用愁
2007/02/20 Javascript
Javascript根据指定下标或对象删除数组元素
2012/12/21 Javascript
Jquery 表单验证类介绍与实例
2013/06/09 Javascript
Jquery 例外被抛出且未被接住原因介绍
2013/09/04 Javascript
js中substring和substr两者区别和使用方法
2015/11/09 Javascript
全面了解javascript三元运算符
2016/06/27 Javascript
jQuery EasyUI datagrid在翻页以后仍能记录被选中行的实现代码
2016/08/15 Javascript
jQuery UI插件实现百度提词器效果
2016/11/21 Javascript
JavaScript使用Ajax上传文件的示例代码
2017/08/10 Javascript
JS实现基于拖拽改变物体大小的方法
2018/01/23 Javascript
微信小程序实现图片翻转效果的实例代码
2019/09/20 Javascript
JavaScript实现手机号码 3-4-4格式并控制新增和删除时光标的位置
2020/06/02 Javascript
Vue-cli assets SubDirectory及PublicPath区别详解
2020/08/18 Javascript
vue切换菜单取消未完成接口请求的案例
2020/11/13 Javascript
Python实现远程调用MetaSploit的方法
2014/08/22 Python
使用python实现正则匹配检索远端FTP目录下的文件
2015/03/25 Python
python基础教程之分支、循环简单用法
2016/06/16 Python
python实现感知器
2017/12/19 Python
python+pillow绘制矩阵盖尔圆简单实例
2018/01/16 Python
Python中创建二维数组
2018/10/17 Python
Python使用mongodb保存爬取豆瓣电影的数据过程解析
2019/08/14 Python
python3实现的zip格式压缩文件夹操作示例
2019/08/17 Python
使用Python和OpenCV检测图像中的物体并将物体裁剪下来
2019/10/30 Python
对python中return与yield的区别详解
2020/03/12 Python
HTML5之SVG 2D入门7—SVG元素的重用与引用
2013/01/30 HTML / CSS
计算机专业自荐信
2013/10/14 职场文书
上课说话检讨书大全
2014/01/22 职场文书
家长会主持词
2014/03/26 职场文书
战略合作意向书范本
2014/04/01 职场文书
小学生爱国演讲稿
2014/04/25 职场文书
家长学校培训材料
2014/08/20 职场文书
5.12护士节活动总结
2015/02/10 职场文书
2019行政前台转正申请书范文3篇
2019/08/15 职场文书