栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas ix &iloc &loc的区别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas ix &iloc &loc的区别

一开始自学Python的numpy、pandas时候,索引和切片把我都给弄晕了,特别是numpy的切片索引、布尔索引和花式索引,简直就是大乱斗。但是最近由于版本的问题,从之前的Python2.7改用Python3.6 了,在3.6中提供了loc和iloc两种索引方法,把ix这个方法给划分开来了,所以很有必要做个总结和对比。

  • loc——通过行标签索引行数据
  • iloc——通过行号索引行数据
  • ix——通过行标签或者行号索引行数据(基于loc和iloc 的混合)

同理,索引列数据也是如此!

举例说明:

1、分别使用loc、iloc、ix 索引第一行的数据:

(1)loc

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.Dataframe(data,index=index,columns=columns)#生成一个数据框

#print df.loc['a']
'''
c  1
d  2
e  3
'''

print df.loc[0]
#这个就会出现错误
'''
TypeError: cannot do label indexing on  
with these indexers [1] of 
'''

(2)iloc

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.Dataframe(data,index=index,columns=columns)#生成一个数据框

print df.iloc[0]
'''
c  1
d  2
e  3
'''
print df.iloc['a']
'''
TypeError: cannot do positional indexing on  
with these indexers [a] of 
'''

(3)ix

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.Dataframe(data,index=index,columns=columns)#生成一个数据框

print df.ix[0]
'''
c  1
d  2
e  3
'''
print df.ix['a']
'''
c  1
d  2
e  3
'''

2、分别使用loc、iloc、ix 索引第一列的数据:

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.Dataframe(data,index=index,columns=columns)#生成一个数据框

print df.loc[:,['c']]

print df.iloc[:,[0]]

print df.ix[:,['c']]

print df.ix[:,[0]]
#结果都为
'''
  c
a 1
b 4
'''

3、分别使用loc、iloc、ix 索引多行的数据:

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.Dataframe(data,index=index,columns=columns)#生成一个数据框

print df.loc['a':'b']

print df.iloc[0:1]

print df.ix['a':'b']

print df.ix[0:1]
#结果都为
'''
  c d e
a 1 2 3
b 4 5 6
'''

4、分别使用loc、iloc、ix 索引多列的数据:

import pandas as pd
data=[[1,2,3],[4,5,6]]
index=['a','b']#行号
columns=['c','d','e']#列号
df=pd.Dataframe(data,index=index,columns=columns)#生成一个数据框

print df.loc[:,'c':'d']

print df.iloc[:,0:2]

print df.ix[:,'c':'d']

print df.ix[:,0:2]
#结果都为
'''
  c d
a 1 2
b 4 5
'''

5、loc、iloc、ix使用切片的区别

loc、iloc、ix对于切片的索引数据就两种情况,按照标签切片索引和按照位置编号切片索引

In [20]: df.loc['ind0':'ind3']
Out[20]: 
   col0 col1 col2 col3 col4
ind0   0   1   2   3   4
ind1   5   6   7   8   9
ind2  10  11  12  13  14
ind3  15  16  17  18  19

In [21]: df.iloc[0:3]
Out[21]: 
   col0 col1 col2 col3 col4
ind0   0   1   2   3   4
ind1   5   6   7   8   9
ind2  10  11  12  13  14

区别不在于用哪种方法,而是通过标签索引将会将切片末端包含进去,通过位置编号索引不会讲切片末端包含进去。同样的都是第一行到第四行,通过loc就会把1,2,3,4行都提取出来,通过iloc就只能把1,2,3行提取出来。ix方法也是一样,知识方法不同而已。

In [23]: df.ix['ind0':'ind3']
Out[23]: 
   col0 col1 col2 col3 col4
ind0   0   1   2   3   4
ind1   5   6   7   8   9
ind2  10  11  12  13  14
ind3  15  16  17  18  19

In [24]: df.ix[0:3]
Out[24]: 
   col0 col1 col2 col3 col4
ind0   0   1   2   3   4
ind1   5   6   7   8   9
ind2  10  11  12  13  14

 对于列的切片跟行的一样。

这里讨论了基本的索引和切片,如果有用词不当的地方请提出来,我将积极改正,或者有其他有关花式索引、布尔索引的问题也可以大家一起讨论讨论!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/27582.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号