详解pandas中iloc, loc和ix的区别和联系
作者:anshuai_aw1 发布时间:2023-03-20 18:56:01
Pandas库十分强大,但是对于切片操作iloc, loc和ix,很多人对此十分迷惑,因此本篇博客利用例子来说明这3者之一的区别和联系,尤其是iloc和loc。
对于ix,由于其操作有些复杂,我在另外一篇博客专门详细介绍ix。
首先,介绍这三种方法的概述:
loc gets rows (or columns) with particular labels from the index. loc从索引中获取具有特定标签的行(或列)。这里的关键是:标签。标签的理解就是name名字。
iloc gets rows (or columns) at particular positions in the index (so it only takes integers). iloc在索引中的特定位置获取行(或列)(因此它只接受整数)。这里的关键是:位置。位置的理解就是排第几个。
ix usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index. ix通常会尝试像loc一样行为,但如果索引中不存在标签,则会退回到像iloc一样的行为。(这句话有些绕口,没关系,不明白可以看这里)
接下来,举几个例子说明:
1 loc
其实,对于loc始终坚持一个原则:loc是基于label进行索引的!
import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c'])
df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
print(df1)
print(df2)
'''
df1:
a b c
0 1 2 3
1 4 5 6
2 7 8 9
df2:
a b c
e 1 2 3
f 4 5 6
g 7 8 9
'''
# loc索引行,label是整型数字
print(df1.loc[0])
'''
a 1
b 2
c 3
Name: 0, dtype: int64
'''
# loc索引行,label是字符型
print(df2.loc['e'])
'''
a 1
b 2
c 3
Name: 0, dtype: int64
'''
# 如果对df2这么写:df2.loc[0]会报错,因为loc索引的是label,显然在df2的行的名字中没有叫0的。
print(df2.loc[0])
'''
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''
# loc索引多行数据
print(df1.loc[1:])
'''
a b c
1 4 5 6
2 7 8 9
'''
# loc索引多列数据
print(df1.loc[:,['a', 'b']])
'''
a b
0 1 2
1 4 5
2 7 8
'''
# df1.loc[:,0:2]这么写报错, 因为loc索引的是label,显然在df1的列的名字中没有叫0,1和2的。
print(df1.loc[:,0:2])
'''
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''
# locs索引某些行某些列
print(df1.loc[0:2, ['a', 'b']])
'''
a b
0 1 2
1 4 5
2 7 8
'''
2 iloc
其实,对于iloc始终坚持一个原则:iloc是基于position进行索引的!
import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c'])
df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c'])
print(df1)
print(df2)
'''
df1:
a b c
0 1 2 3
1 4 5 6
2 7 8 9
df2:
a b c
e 1 2 3
f 4 5 6
g 7 8 9
'''
# iloc索引行,label是整型数字
print(df1.iloc[0])
'''
a 1
b 2
c 3
Name: 0, dtype: int64
'''
# iloc索引行,label是字符型。如果按照loc的写法来写应该是:df2.iloc['e'],显然这样报错,因为iloc不认识label,它是基于位置的。
print(df2.iloc['e'])
'''
TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [e] of <class 'str'>
'''
# iloc索引行,label是字符型。正确的写法应该如下:
# 也就说,不论index是什么类型的,iloc只能写位置,也就是整型数字。
print(df2.iloc[0])
'''
a 1
b 2
c 3
Name: e, dtype: int64
'''
# iloc索引多行数据
print(df1.iloc[1:])
'''
a b c
1 4 5 6
2 7 8 9
'''
# iloc索引多列数据
# 如果如下写法,报错。
print(df1.iloc[:,['a', 'b']])
'''
TypeError: cannot perform reduce with flexible type
'''
# iloc索引多列数据, 正确写法如下:
print(df1.iloc[:,0:2])
'''
a b
0 1 2
1 4 5
2 7 8
'''
# iloc索引某些行某些列
print(df1.iloc[0:2, 0:1])
'''
a
0 1
1 4
'''
3 ix
ix的操作比较复杂,在pandas版本0.20.0及其以后版本中,ix已经不被推荐使用,建议采用iloc和loc实现ix。
如有对ix的使用比较感兴趣的朋友可以参考这篇博客。
来源:https://blog.csdn.net/anshuai_aw1/article/details/82802769


猜你喜欢
- 通常的聊天室所采用的程序,也就是Chat程序了,其基本结构原理是不会采用到数据库的。那究竟采用什么技术呢?我们知道ASP变量当中Sessio
- 前言语音合成技术能将用户输入的文字,转换成流畅自然的语音输出,并且可以支持语速、音调、音量设置,打破传统文字式人机交互的方式,让人机沟通更自
- 将一个 awk 脚本移植到 Python 主要在于代码风格而不是转译。脚本是解决问题的有效方法,而 awk 是编写脚本的出色语言。它特别擅长
- 我就废话不多说了,还是直接上代码吧! url = "http://%s:%s/api-token-auth/" % (i
- 出差到了中国雅虎,这里的风格和淘宝很不一样。和雅虎一比,淘宝的办公环境就是个菜市场,闹哄哄,到处是人,在走道里狂奔乱窜,在每个会议室争得面红
- 前言最近微信小游戏跳一跳大热,自己也是中毒颇久,无奈手残最高分只拿到200分。无意间看到教你用Python来玩微信跳一跳一文,在电脑上利用a
- 1 如何创建项目数据库首先,在虚拟机数据库中建立一个与项目同名的数据库,方便管理。(django_test) bd@DF:~$ mysql
- 0x00:事先说明你已经攻陷了对方主机且获得了最高权限。对方的本地防火墙会丢弃所有的外来数据包。这个后门不会仅绑定在某一个端口上。这段代码很
- JAVA程序想要访问数据库,需要进行如下准备:1.安装一个数据库(这里使用mysql免安装版)2.下载该数据库的驱动包(这里使用mysql官
- Django自带有个强大的后天管理系统,接下来我就给大家介绍一下x的admin一些强大的操作及后台美化。首先给大家介绍一些xadmin的注册
- 简单的并发控制利用 channel 的缓冲设定,我们就可以来实现并发的限制。我们只要在执行并发的同时,往一个带有缓冲的 chann
- tensorflow在1.4版本引入了keras,封装成库。现想将keras版本的GRU代码移植到TensorFlow中,看到TensorF
- 自动发送邮件我们把报表做出来以后一般都是需要发给别人查看,对于一些每天需要发的报表或者是需要一次发送多份的报表,这个时候可以考虑借助Pyth
- 手动备份1)cmd控制台:mysqldump -uroot -proot 数据库名 [表名1,表名2...] > 文件路径比如:把 d
- 我就废话不多说了,大家还是直接看代码吧~from keras.applications.vgg16 import VGG16#直接导入已经训
- kruskal算法基本思路:先对边按权重从小到大排序,先选取权重最小的一条边,如果该边的两个节点均为不同的分量,则加入到最小生成树,否则计算
- 因为要学着写渗透工具,这几天都在上python编程基础课,听得我打瞌睡,毕竟以前学过嘛。最后sherry老师留了作业,其中一道题是这样的:题
- 前言urllib、urllib2、urllib3、httplib、httplib2 都是和 HTTP 相关的 Python 模块,看名字就觉
- 本文代码来之《数据分析与挖掘实战》,在此基础上补充完善了一下~代码是基于SVM的分类器Python实现,原文章节题目和code关系不大,或者
- 代码如下: function HandleTabKey(evt) {