Pandas資料框索引函式 iloc、loc和ix學習使用
阿新 • • 發佈:2018-12-08
在資料科學領域python逐漸火熱起來,超越了原有R的地位,這裡豐富的第三方包的貢獻功不可沒,數值計算中Numpy和Pandas絕對是必備的神器,最近使用到Pandas來做資料的操作,今天正好有時間就簡單地總結記錄一點自己學習使用Pandas的體會,主要是對幾個主要的資料框索引函式進行講解和使用,相關的解釋都已經在程式碼裡面了,就不再多解釋了,配合著我給出來的例子相信還是很容易理解的,下面是具體的實踐:
#!usr/bin/env python #encoding:utf-8 from __future__ import division ''' __Author__:沂水寒城 功能:Pandas資料框索引函式 iloc、loc和ix學習使用 ''' ''' Pandas庫中有iloc和loc以及ix可以用來索引資料,抽取資料。 loc函式是通過行標籤索引行資料 iloc函式是通過行號索引行資料 ix函式是通過行標籤或者行號索引行資料,簡單來說就是loc和iloc的混合體 iloc主要使用數字來索引資料,而不能使用字元型的標籤來索引資料。而loc則剛好相反,只能使用字元型標籤來索引資料, 不能使用數字來索引資料,不過有特殊情況,當資料框dataframe的行標籤或者列標籤為數字,loc就可以來其來索引。 ix是一種混合索引,字元型標籤和整型資料索引都可以 ''' import sys import pandas as pd import numpy as np reload(sys) sys.setdefaultencoding('utf-8') def testFunc(): ''' ''' data=np.arange(36).reshape(6,6) print 'data:' print data df=pd.DataFrame(data) print 'dataFrame:' print df print '-*'*30 print df.loc[1] print '-*'*30 print df.iloc[1] print '-*'*30 print df.loc[:,[0,4]] print '-*'*30 print df.iloc[:,[0,4]] print '-*'*30 print u"將數值型索引替換為字元型索引:" df.index=['ID','Number','Height','Weight','Circle','Space'] print df print '-*'*30 try: print df.loc[0] except Exception,e: print 'Exception: ',e print '-*'*30 try: print df.iloc['Height'] except Exception,e: print 'Exception: ',e print '-|=|'*30 print df.iloc[0] print df.loc['Circle'] print '-*'*30 print u"將數值型列索引替換為字元型列索引:" df.columns=['zero','one','two','three','four','five'] print df print '-*'*30 print df.loc[:,'zero'] print '-*'*30 try: print df.iloc[:,'one'] except Exception,e: print 'Exception: ',e print '-*'*30 print u"ix抽取資料示例" print df.ix[5] print '-*'*30 print df.ix[:,'three'] print '-*'*30 print df.ix[:,'one'] print '-*'*30 print df.ix[:,'five'] print u"獲取多行資料:" print df.loc['Height':'Space'] print df.iloc[2:] print df.ix['Height':'Space'] print df.ix[2:] print '*%'*40 print u"獲取多列資料:" print df.loc[:,'zero':'four'] print df.iloc[:,0:5] print df.ix[:,'zero':'four'] print df.ix[:,0:5] if __name__=='__main__': testFunc()
結果如下:
data: [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29] [30 31 32 33 34 35]] dataFrame: 0 1 2 3 4 5 0 0 1 2 3 4 5 1 6 7 8 9 10 11 2 12 13 14 15 16 17 3 18 19 20 21 22 23 4 24 25 26 27 28 29 5 30 31 32 33 34 35 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 0 6 1 7 2 8 3 9 4 10 5 11 Name: 1, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 0 6 1 7 2 8 3 9 4 10 5 11 Name: 1, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 0 4 0 0 4 1 6 10 2 12 16 3 18 22 4 24 28 5 30 34 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 0 4 0 0 4 1 6 10 2 12 16 3 18 22 4 24 28 5 30 34 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 將數值型索引替換為字元型索引: 0 1 2 3 4 5 ID 0 1 2 3 4 5 Number 6 7 8 9 10 11 Height 12 13 14 15 16 17 Weight 18 19 20 21 22 23 Circle 24 25 26 27 28 29 Space 30 31 32 33 34 35 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Exception: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <type 'int'> -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Exception: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [Height] of <type 'str'> -|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=|-|=| 0 0 1 1 2 2 3 3 4 4 5 5 Name: ID, dtype: int32 0 24 1 25 2 26 3 27 4 28 5 29 Name: Circle, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* 將數值型列索引替換為字元型列索引: zero one two three four five ID 0 1 2 3 4 5 Number 6 7 8 9 10 11 Height 12 13 14 15 16 17 Weight 18 19 20 21 22 23 Circle 24 25 26 27 28 29 Space 30 31 32 33 34 35 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ID 0 Number 6 Height 12 Weight 18 Circle 24 Space 30 Name: zero, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* Exception: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ix抽取資料示例 zero 30 one 31 two 32 three 33 four 34 five 35 Name: Space, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ID 3 Number 9 Height 15 Weight 21 Circle 27 Space 33 Name: three, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ID 1 Number 7 Height 13 Weight 19 Circle 25 Space 31 Name: one, dtype: int32 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ID 5 Number 11 Height 17 Weight 23 Circle 29 Space 35 Name: five, dtype: int32 獲取多行資料: zero one two three four five Height 12 13 14 15 16 17 Weight 18 19 20 21 22 23 Circle 24 25 26 27 28 29 Space 30 31 32 33 34 35 zero one two three four five Height 12 13 14 15 16 17 Weight 18 19 20 21 22 23 Circle 24 25 26 27 28 29 Space 30 31 32 33 34 35 zero one two three four five Height 12 13 14 15 16 17 Weight 18 19 20 21 22 23 Circle 24 25 26 27 28 29 Space 30 31 32 33 34 35 zero one two three four five Height 12 13 14 15 16 17 Weight 18 19 20 21 22 23 Circle 24 25 26 27 28 29 Space 30 31 32 33 34 35 *%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*%*% 獲取多列資料: zero one two three four ID 0 1 2 3 4 Number 6 7 8 9 10 Height 12 13 14 15 16 Weight 18 19 20 21 22 Circle 24 25 26 27 28 Space 30 31 32 33 34 zero one two three four ID 0 1 2 3 4 Number 6 7 8 9 10 Height 12 13 14 15 16 Weight 18 19 20 21 22 Circle 24 25 26 27 28 Space 30 31 32 33 34 zero one two three four ID 0 1 2 3 4 Number 6 7 8 9 10 Height 12 13 14 15 16 Weight 18 19 20 21 22 Circle 24 25 26 27 28 Space 30 31 32 33 34 zero one two three four ID 0 1 2 3 4 Number 6 7 8 9 10 Height 12 13 14 15 16 Weight 18 19 20 21 22 Circle 24 25 26 27 28 Space 30 31 32 33 34
在實際的工作中,如果能夠很熟練地掌握這些常用的資料操作函式對於效率的提升相信還是很有幫助的。