【pandas小记】pandas中的“标签”索引 与 “整数”索引

【Python】Pandas中的“标签”索引 与 “整数”索引

一,索引

pandas在构建Series和DataFrame时都会创建一个索引序列,类似于标签标示每个数据,不同的是,DataFrame会有行索引和列索引。注意,这里的索引类似于标记key,通过这个key可以定位到对应的value,也可以看做一个字典

In [93]: obj = pd.Series(np.arange(1,5),index=['one','two','three','four'])                                              
In [94]: obj                                                                                                             
Out[94]: 
one      1
two      2
three    3
four     4
dtype: int64
In [95]: obj['one']    #通过标签key定位value                                                                                                   
Out[95]: 1

二,通过索引选取数据

1,Pandas的索引与标准Python中的索引功能类似,只不过Pandas的索引值不仅仅是整数,还可以是前面说到的标签。

In [97]: obj[[0,1]]    #整数索引从0开始                                                                                                  
Out[97]: 
one    1
two    2
dtype: int64
In [98]: obj[['one','two']]  #轴标签索引                                                                                            
Out[98]: 
one    1
two    2
dtype: int64
In [100]: obj[[-1,-3]]       #负整数索引,从-1开始                                                                                                
Out[100]: 
four    4
two     2
dtype: int64
In [101]: obj[['four','two']]                                                                                            
Out[101]: 
four    4
two     2
dtype: int64

整数索引与标准Python的一样都是从0开始,负整数索引从-1开始。除此之外Pandas中的索引也支持切片。

In [105]: a = pd.DataFrame(np.arange(16).reshape(4,4),index=['a','b','c','d'],columns=['one','two','three','four'])      
In [106]: a                                                                                                              
Out[106]: 
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
c    8    9     10    11
d   12   13     14    15
In [107]: a[:2]                                                                                                          
Out[107]: 
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
In [108]: a[:'c']                                                                                                        
Out[108]: 
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
c    8    9     10    11

可以看出区别,用整数索引进行切片时,左边等于右边不等于,即是0<= index < 2,而使用标签索引则是’a’<= index<=‘c’。 所以需要注意这两种类型的索引使用。

2,DataFrame还可以使用loc和iloc来选择数据,而两者是根据“标签”或“整数”索引来选择的,

In [112]: a.iloc[:2,:]   #根据整数索引选择数据                                                                                                
Out[112]: 
   one  two  three  four
a    0    1      2     3
b    4    5      6     7

In [113]: a.loc[:'c',:]       #根据标签选择数据                                                                                           
Out[113]: 
   one  two  three  four
a    0    1      2     3
b    4    5      6     7
c    8    9     10    11

loc 和 iloc将标签索引与整数标签区别使用,这要可以避免造成错误。其实在它们之前还有一个ix,可以将这两种标签混合使用。

In [114]: a.ix[:'c',:2]                                                                                                  
/usr/local/bin/ipython:1: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  #!/usr/local/bin/python3.6
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:822: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
Out[114]: 
   one  two
a    0    1
b    4    5
c    8    9

ix 被定义成 deprecated,大概是因为可以混合label和position导致了很多用户问题和bug。所以用标签时就loc,用整数时则用iloc,避免造成歧义。

3,将标签设置为整数时,也会造成歧义。

In [122]: b = pd.DataFrame(np.arange(16).reshape(4,4),index=[0,1,2,3],columns=['one','two','three','four'])              
In [123]: b                                                                                                              
Out[123]: 
   one  two  three  four
0    0    1      2     3
1    4    5      6     7
2    8    9     10    11
3   12   13     14    15
In [124]: b[:2]       #整数  or 标签?                                                                                                   
Out[124]: 
   one  two  three  four
0    0    1      2     3
1    4    5      6     7

In [125]: b.loc[:2]                                                                                                      
Out[125]: 
   one  two  three  four
0    0    1      2     3
1    4    5      6     7
2    8    9     10    11

In [126]: b.iloc[:2]                                                                                                     
Out[126]: 
   one  two  three  four
0    0    1      2     3
1    4    5      6     7

上面的例子中,b数组使用整数作为行标签,那么就造成歧义了。

b[:2]       #这里是使用整数还是标签?像是使用整数索引  

所以,如果使用整数作为标签,那么在数据选择是使用loc 或者 iloc,这样可以明确知道使用的是哪种类型的索引,从而精确地选择数据。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章