pandas ix &iloc &loc的區(qū)別
一開(kāi)始自學(xué)Python的numpy、pandas時(shí)候,索引和切片把我都給弄暈了,特別是numpy的切片索引、布爾索引和花式索引,簡(jiǎn)直就是大亂斗。但是最近由于版本的問(wèn)題,從之前的Python2.7改用Python3.6 了,在3.6中提供了loc和iloc兩種索引方法,把ix這個(gè)方法給劃分開(kāi)來(lái)了,所以很有必要做個(gè)總結(jié)和對(duì)比。
- loc——通過(guò)行標(biāo)簽索引行數(shù)據(jù)
- iloc——通過(guò)行號(hào)索引行數(shù)據(jù)
- ix——通過(guò)行標(biāo)簽或者行號(hào)索引行數(shù)據(jù)(基于loc和iloc 的混合)
同理,索引列數(shù)據(jù)也是如此!
舉例說(shuō)明:
1、分別使用loc、iloc、ix 索引第一行的數(shù)據(jù):
(1)loc
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行號(hào) columns=['c','d','e']#列號(hào) df=pd.DataFrame(data,index=index,columns=columns)#生成一個(gè)數(shù)據(jù)框 #print df.loc['a'] ''' c 1 d 2 e 3 ''' print df.loc[0] #這個(gè)就會(huì)出現(xiàn)錯(cuò)誤 ''' TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers [1] of <type 'int'> '''
(2)iloc
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行號(hào) columns=['c','d','e']#列號(hào) df=pd.DataFrame(data,index=index,columns=columns)#生成一個(gè)數(shù)據(jù)框 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
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行號(hào) columns=['c','d','e']#列號(hào) df=pd.DataFrame(data,index=index,columns=columns)#生成一個(gè)數(shù)據(jù)框 print df.ix[0] ''' c 1 d 2 e 3 ''' print df.ix['a'] ''' c 1 d 2 e 3 '''
2、分別使用loc、iloc、ix 索引第一列的數(shù)據(jù):
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行號(hào) columns=['c','d','e']#列號(hào) df=pd.DataFrame(data,index=index,columns=columns)#生成一個(gè)數(shù)據(jù)框 print df.loc[:,['c']] print df.iloc[:,[0]] print df.ix[:,['c']] print df.ix[:,[0]] #結(jié)果都為 ''' c a 1 b 4 '''
3、分別使用loc、iloc、ix 索引多行的數(shù)據(jù):
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行號(hào) columns=['c','d','e']#列號(hào) df=pd.DataFrame(data,index=index,columns=columns)#生成一個(gè)數(shù)據(jù)框 print df.loc['a':'b'] print df.iloc[0:1] print df.ix['a':'b'] print df.ix[0:1] #結(jié)果都為 ''' c d e a 1 2 3 b 4 5 6 '''
4、分別使用loc、iloc、ix 索引多列的數(shù)據(jù):
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行號(hào) columns=['c','d','e']#列號(hào) df=pd.DataFrame(data,index=index,columns=columns)#生成一個(gè)數(shù)據(jù)框 print df.loc[:,'c':'d'] print df.iloc[:,0:2] print df.ix[:,'c':'d'] print df.ix[:,0:2] #結(jié)果都為 ''' c d a 1 2 b 4 5 '''
5、loc、iloc、ix使用切片的區(qū)別
loc、iloc、ix對(duì)于切片的索引數(shù)據(jù)就兩種情況,按照標(biāo)簽切片索引和按照位置編號(hào)切片索引
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
區(qū)別不在于用哪種方法,而是通過(guò)標(biāo)簽索引將會(huì)將切片末端包含進(jìn)去,通過(guò)位置編號(hào)索引不會(huì)講切片末端包含進(jìn)去。同樣的都是第一行到第四行,通過(guò)loc就會(huì)把1,2,3,4行都提取出來(lái),通過(guò)iloc就只能把1,2,3行提取出來(lái)。ix方法也是一樣,知識(shí)方法不同而已。
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
對(duì)于列的切片跟行的一樣。
這里討論了基本的索引和切片,如果有用詞不當(dāng)?shù)牡胤秸?qǐng)?zhí)岢鰜?lái),我將積極改正,或者有其他有關(guān)花式索引、布爾索引的問(wèn)題也可以大家一起討論討論!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)破解猜數(shù)游戲算法示例
這篇文章主要介紹了Python實(shí)現(xiàn)破解猜數(shù)游戲算法,簡(jiǎn)單描述了猜數(shù)游戲的原理,并結(jié)合具體實(shí)例形式分析了Python破解猜數(shù)游戲的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09Django+Ajax異步刷新/定時(shí)自動(dòng)刷新實(shí)例詳解
AJAX是前端技術(shù)的集合,包括JavaScript、XML、HTML、CSS等,下面這篇文章主要給大家介紹了關(guān)于Django+Ajax異步刷新/定時(shí)自動(dòng)刷新的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10python manim實(shí)現(xiàn)排序算法動(dòng)畫(huà)示例
這篇文章主要為大家介紹了python manim實(shí)現(xiàn)排序算法動(dòng)畫(huà)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08對(duì)Python通過(guò)pypyodbc訪(fǎng)問(wèn)Access數(shù)據(jù)庫(kù)的方法詳解
今天小編就為大家分享一篇對(duì)Python通過(guò)pypyodbc訪(fǎng)問(wèn)Access數(shù)據(jù)庫(kù)的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10