脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pandas ix loc的区别

pandas ix loc的区别

2021-05-14 00:03memoryqiu Python

这篇文章主要介绍了pandas ix &iloc &loc的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

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

举例说明:

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

(1)loc

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 <class 'pandas.indexes.base.Index'>
with these indexers [1] of <type 'int'>
'''

(2)iloc

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 <class 'pandas.indexes.base.Index'>
with these indexers [a] of <type 'str'>
'''

(3)ix

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 索引第一列的数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 索引多行的数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 索引多列的数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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对于切片的索引数据就两种情况,按照标签切片索引和按照位置编号切片索引

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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方法也是一样,知识方法不同而已。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/sinat_25873421/article/details/80634846

延伸 · 阅读

精彩推荐