详谈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 Tkinter GUI编程入门介绍
Mar 10 Python
python实现从网络下载文件并获得文件大小及类型的方法
Apr 28 Python
Python出现segfault错误解决方法
Apr 16 Python
解决python selenium3启动不了firefox的问题
Oct 13 Python
python requests 库请求带有文件参数的接口实例
Jan 03 Python
几行Python代码爬取3000+上市公司的信息
Jan 24 Python
python多进程重复加载的解决方式
Dec 13 Python
python交互模式基础知识点学习
Jun 18 Python
解决导入django_filters不成功问题No module named 'django_filter'
Jul 15 Python
python3将变量输入的简单实例
Aug 19 Python
win10+anaconda安装yolov5的方法及问题解决方案
Apr 29 Python
Python3中PyQt5简单实现文件打开及保存
Jun 10 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
四月新番又没了,《Re:从零开始的异世界生活》第二季延期至7月播出
2020/05/06 日漫
PHP 函数学习简单小结
2010/07/08 PHP
PHP中应该避免使用同名变量(拆分临时变量)
2015/04/03 PHP
PHP+redis实现添加处理投票的方法
2015/11/14 PHP
javascript编程起步(第二课)
2007/02/27 Javascript
Node.js实现文件上传
2016/07/05 Javascript
JS Canvas定时器模拟动态加载动画
2016/09/17 Javascript
JS库 Highlightjs 添加代码行号的实现代码
2017/09/13 Javascript
vue实现密码显示隐藏切换功能
2018/02/23 Javascript
vue src动态加载请求获取图片的方法
2018/10/17 Javascript
vue计算属性computed的使用方法示例
2019/03/13 Javascript
实用的Vue开发技巧
2019/05/30 Javascript
VUE动态生成word的实现
2020/07/26 Javascript
用python分割TXT文件成4K的TXT文件
2009/05/23 Python
python实现井字棋游戏
2020/03/30 Python
Python中用post、get方式提交数据的方法示例
2017/09/22 Python
浅谈Python的条件判断语句if/else语句
2019/03/21 Python
python查看文件大小和文件夹内容的方法
2019/07/08 Python
Pandas之groupby( )用法笔记小结
2019/07/23 Python
Django实现分页显示效果
2019/10/31 Python
pytorch如何冻结某层参数的实现
2020/01/10 Python
Python基于requests库爬取网站信息
2020/03/02 Python
Django import export实现数据库导入导出方式
2020/04/03 Python
python模拟哔哩哔哩滑块登入验证的实现
2020/04/24 Python
css3 图片圆形显示 如何CSS将正方形图片显示为圆形图片布局
2014/10/10 HTML / CSS
JBL加拿大官方商店:扬声器、耳机等
2020/10/23 全球购物
大学生个人实习的自我评价
2014/02/15 职场文书
大学生暑期实践感言
2014/02/26 职场文书
2014小学植树节活动总结
2014/03/10 职场文书
如何写一份好的英文求职信
2014/03/19 职场文书
项目采购员岗位职责
2014/04/15 职场文书
大学生村官演讲稿
2014/04/25 职场文书
异地年检委托书范本
2014/09/24 职场文书
转让协议书
2015/01/27 职场文书
综合素质自我评价评语
2015/03/06 职场文书
Spring中bean集合注入的方法详解
2022/07/07 Java/Android