詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系
Pandas庫(kù)十分強(qiáng)大,但是對(duì)于切片操作iloc, loc和ix,很多人對(duì)此十分迷惑,因此本篇博客利用例子來(lái)說(shuō)明這3者之一的區(qū)別和聯(lián)系,尤其是iloc和loc。
對(duì)于ix,由于其操作有些復(fù)雜,我在另外一篇博客專門(mén)詳細(xì)介紹ix。
首先,介紹這三種方法的概述:
- loc gets rows (or columns) with particular labels from the index. loc從索引中獲取具有特定標(biāo)簽的行(或列)。這里的關(guān)鍵是:標(biāo)簽。標(biāo)簽的理解就是name名字。
- iloc gets rows (or columns) at particular positions in the index (so it only takes integers). iloc在索引中的特定位置獲取行(或列)(因此它只接受整數(shù))。這里的關(guān)鍵是:位置。位置的理解就是排第幾個(gè)。
- ix usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index. ix通常會(huì)嘗試像loc一樣行為,但如果索引中不存在標(biāo)簽,則會(huì)退回到像iloc一樣的行為。(這句話有些繞口,沒(méi)關(guān)系,不明白可以看這里)
接下來(lái),舉幾個(gè)例子說(shuō)明:
1 loc
其實(shí),對(duì)于loc始終堅(jiān)持一個(gè)原則:loc是基于label進(jìn)行索引的!
import pandas as pd df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c']) df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c']) print(df1) print(df2) ''' df1: a b c 0 1 2 3 1 4 5 6 2 7 8 9 df2: a b c e 1 2 3 f 4 5 6 g 7 8 9 ''' # loc索引行,label是整型數(shù)字 print(df1.loc[0]) ''' a 1 b 2 c 3 Name: 0, dtype: int64 ''' # loc索引行,label是字符型 print(df2.loc['e']) ''' a 1 b 2 c 3 Name: 0, dtype: int64 ''' # 如果對(duì)df2這么寫(xiě):df2.loc[0]會(huì)報(bào)錯(cuò),因?yàn)閘oc索引的是label,顯然在df2的行的名字中沒(méi)有叫0的。 print(df2.loc[0]) ''' TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'> ''' # loc索引多行數(shù)據(jù) print(df1.loc[1:]) ''' a b c 1 4 5 6 2 7 8 9 ''' # loc索引多列數(shù)據(jù) print(df1.loc[:,['a', 'b']]) ''' a b 0 1 2 1 4 5 2 7 8 ''' # df1.loc[:,0:2]這么寫(xiě)報(bào)錯(cuò), 因?yàn)閘oc索引的是label,顯然在df1的列的名字中沒(méi)有叫0,1和2的。 print(df1.loc[:,0:2]) ''' TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'> ''' # locs索引某些行某些列 print(df1.loc[0:2, ['a', 'b']]) ''' a b 0 1 2 1 4 5 2 7 8 '''
2 iloc
其實(shí),對(duì)于iloc始終堅(jiān)持一個(gè)原則:iloc是基于position進(jìn)行索引的!
import pandas as pd df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c']) df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c']) print(df1) print(df2) ''' df1: a b c 0 1 2 3 1 4 5 6 2 7 8 9 df2: a b c e 1 2 3 f 4 5 6 g 7 8 9 ''' # iloc索引行,label是整型數(shù)字 print(df1.iloc[0]) ''' a 1 b 2 c 3 Name: 0, dtype: int64 ''' # iloc索引行,label是字符型。如果按照l(shuí)oc的寫(xiě)法來(lái)寫(xiě)應(yīng)該是:df2.iloc['e'],顯然這樣報(bào)錯(cuò),因?yàn)閕loc不認(rèn)識(shí)label,它是基于位置的。 print(df2.iloc['e']) ''' TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [e] of <class 'str'> ''' # iloc索引行,label是字符型。正確的寫(xiě)法應(yīng)該如下: # 也就說(shuō),不論index是什么類(lèi)型的,iloc只能寫(xiě)位置,也就是整型數(shù)字。 print(df2.iloc[0]) ''' a 1 b 2 c 3 Name: e, dtype: int64 ''' # iloc索引多行數(shù)據(jù) print(df1.iloc[1:]) ''' a b c 1 4 5 6 2 7 8 9 ''' # iloc索引多列數(shù)據(jù) # 如果如下寫(xiě)法,報(bào)錯(cuò)。 print(df1.iloc[:,['a', 'b']]) ''' TypeError: cannot perform reduce with flexible type ''' # iloc索引多列數(shù)據(jù), 正確寫(xiě)法如下: print(df1.iloc[:,0:2]) ''' a b 0 1 2 1 4 5 2 7 8 ''' # iloc索引某些行某些列 print(df1.iloc[0:2, 0:1]) ''' a 0 1 1 4 '''
3 ix
ix的操作比較復(fù)雜,在pandas版本0.20.0及其以后版本中,ix已經(jīng)不被推薦使用,建議采用iloc和loc實(shí)現(xiàn)ix。
如有對(duì)ix的使用比較感興趣的朋友可以參考這篇博客。
到此這篇關(guān)于詳解pandas中iloc, loc和ix的區(qū)別和聯(lián)系的文章就介紹到這了,更多相關(guān)pandas iloc loc ix內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python連接MySQL數(shù)據(jù)庫(kù)進(jìn)行編程的步驟詳解
Python數(shù)據(jù)庫(kù)編程可以使用多種模塊與API,例如SQLite、MySQL、PostgreSQL等,本教程將重點(diǎn)介紹使用Python連接MySQL數(shù)據(jù)庫(kù)進(jìn)行編程,需要的朋友可以參考下2023-06-06
老生常談Python startswith()函數(shù)與endswith函數(shù)
下面小編就為大家?guī)?lái)一篇老生常談Python startswith()函數(shù)與endswith函數(shù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Python實(shí)現(xiàn)解析Html的方法與對(duì)比
在最近需要的需求中,需要?python?獲取網(wǎng)頁(yè)內(nèi)容,并從html中獲取到想要的內(nèi)容,本文主要介紹了兩種常用方法并進(jìn)行了對(duì)比,感興趣的可以了解下2024-03-03
python?list與numpy數(shù)組效率對(duì)比
這篇文章主要介紹了python?list與numpy數(shù)組效率對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python實(shí)現(xiàn)冒泡排序的簡(jiǎn)單應(yīng)用示例
這篇文章主要介紹了Python實(shí)現(xiàn)冒泡排序的簡(jiǎn)單應(yīng)用,結(jié)合實(shí)例形式分析了Python基于冒泡排序?qū)崿F(xiàn)的輸入字符串?dāng)?shù)字排序與運(yùn)算操作,需要的朋友可以參考下2017-12-12
用于統(tǒng)計(jì)項(xiàng)目中代碼總行數(shù)的Python腳本分享
這篇文章主要介紹了用于統(tǒng)計(jì)項(xiàng)目中代碼總行數(shù)的Python腳本分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
Python標(biāo)準(zhǔn)庫(kù)與第三方庫(kù)詳解
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)與第三方庫(kù),需要的朋友可以參考下2014-07-07
Python?dateutil庫(kù)簡(jiǎn)化日期時(shí)間處理利器使用場(chǎng)景實(shí)踐
在Python中,處理日期和時(shí)間是常見(jiàn)的任務(wù)之一,dateutil庫(kù)是Python標(biāo)準(zhǔn)庫(kù)中datetime模塊的擴(kuò)展,提供了許多方便的工具和函數(shù),簡(jiǎn)化了日期和時(shí)間的操作2023-12-12

