pandas loc iloc ix用法詳細(xì)分析
1.什么是label
pandas處理數(shù)據(jù)時(shí),我們會(huì)經(jīng)常看到dataframe結(jié)構(gòu)使用loc, iloc, ix等方法。那么這些方法到底有啥區(qū)別,下面我們來(lái)進(jìn)行詳細(xì)分析。
首先我們先明確一點(diǎn),這幾個(gè)方法都可以用來(lái)過(guò)濾dataframe的行列。他們的不同,主要還是使用方式的不同。
在分析之前,我們先來(lái)明確一下標(biāo)簽label的概念。為了方便看得更清楚,先構(gòu)造一個(gè)數(shù)據(jù)集
import pandas as pd def test_loc(): name = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6'] age = [1, 2, 3, 4, 5, 6] country = ['Chi', 'Chi', 'Ame', 'Ame', 'Jp', 'Koe'] city = ['bj', 'sh', 'ny', 'ny', 'tok', 'se'] data = pd.DataFrame({'name': name, 'age': age, 'country': country, 'city': city}) # label print(data)
name age country city 0 n1 1 Chi bj 1 n2 2 Chi sh 2 n3 3 Ame ny 3 n4 4 Ame ny 4 n5 5 Jp tok 5 n6 6 Koe se
上面的data,0,1,2,3,4,5為索引,就是我們的行標(biāo)簽。name ,age,country,city為列名,則是我們的列標(biāo)簽。
2.loc用法
我們先直接上結(jié)論:loc可以基于行列標(biāo)簽對(duì)數(shù)據(jù)進(jìn)行篩選。
下面通過(guò)實(shí)驗(yàn)來(lái)說(shuō)明。
def test_loc(): name = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6'] age = [1, 2, 3, 4, 5, 6] country = ['Chi', 'Chi', 'Ame', 'Ame', 'Jp', 'Koe'] city = ['bj', 'sh', 'ny', 'ny', 'tok', 'se'] data = pd.DataFrame({'name': name, 'age': age, 'country': country, 'city': city}) # 取前幾行 print(data.loc[[0, 1, 2]]) print() print(data.loc[0:2]) print() # 取某幾列 print(data.loc[:, ['name', 'age', 'city']]) print() # 取幾行幾列 print(data.loc[0:2,['name', 'age', 'city']]) print()
2.1 選擇行
loc的整體語(yǔ)法為loc[rows, columns]。逗號(hào)前面部分為選擇的行,后面部分為選擇的列,":"表示全選。
loc[0:2]與loc[[0, 1, 2]]的效果一致,都是表示選取前3行,可以認(rèn)為此時(shí)是通過(guò)行標(biāo)簽選擇數(shù)據(jù)。
2.2 選擇列
實(shí)際操作中,最常見(jiàn)的需求就是選擇某幾列而不是所有數(shù)據(jù)。loc[:, [‘name’, ‘age’, ‘city’]]就是選擇散列,逗號(hào)前面的:表示選擇所有行。
2.3 選擇指定的行列
data.loc[0:2,[‘name’, ‘age’, ‘city’]]這種用法,意思就是選擇前三行的name,age,city這三列。
2.4 loc小結(jié)
1.就像我們一開(kāi)始提到的,loc是基于數(shù)據(jù)行列標(biāo)簽對(duì)數(shù)據(jù)進(jìn)行篩選。
2.針對(duì)行標(biāo)簽選擇時(shí),如果index是默認(rèn)的整數(shù)序列,選擇的時(shí)候包括了末端的一行。
3.":"表示選擇所有行或者所有列。
3.iloc用法
直接上結(jié)論:iloc與loc的不同在于,loc基于數(shù)據(jù)標(biāo)簽進(jìn)行篩選,而iloc基于位置進(jìn)行數(shù)據(jù)篩選,i可以認(rèn)為是integer,即在loc的基礎(chǔ)上,用integer整數(shù)當(dāng)作"索引"
看個(gè)例子
def test_iloc(): name = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6'] age = [1, 2, 3, 4, 5, 6] country = ['Chi', 'Chi', 'Ame', 'Ame', 'Jp', 'Koe'] city = ['bj', 'sh', 'ny', 'ny', 'tok', 'se'] data = pd.DataFrame({'name': name, 'age': age, 'country': country, 'city': city}) # iloc的索引,不包含最后一個(gè) print(data.iloc[0:2]) print() # print(data.iloc[:, 0:2]) print() print(data.iloc[:,[0, 1, 3]]) print() # print(data.iloc[:, ['name', 'city']]) # IndexError: .iloc requires numeric indexers, got ['name' 'city']
最后輸出為:
name age country city
0 n1 1 Chi bj
1 n2 2 Chi shname age
0 n1 1
1 n2 2
2 n3 3
3 n4 4
4 n5 5
5 n6 6name age city
0 n1 1 bj
1 n2 2 sh
2 n3 3 ny
3 n4 4 ny
4 n5 5 tok
5 n6 6 se
iloc[0:2],表示選擇前兩行。注意在iloc中,末端那行不包括。因?yàn)槠鹗妓饕?,所以iloc[0:2]選擇的是第0行與第1行。
data.iloc[:, 0:2]表示選擇前兩列,data.iloc[:,[0, 1, 3]]表示選擇第0,1,3列。
如果我們嘗試用列名篩選數(shù)據(jù),data.iloc[:, [‘name’, ‘city’]]
代碼會(huì)報(bào)錯(cuò)
IndexError: .iloc requires numeric indexers, got ['name' 'city']
上面的錯(cuò)誤信息就很明確的告訴了我們,iloc方法需要numeric indexers。
4.ix
最后一個(gè)ix,是歷史版本的用法。ix的作用,現(xiàn)在用loc,iloc基本都能實(shí)現(xiàn),所以ix也基本上被loc,iloc所代替,現(xiàn)在官方不再推薦使用。
到此這篇關(guān)于pandas loc iloc ix用法詳解的文章就介紹到這了,更多相關(guān)pandas loc iloc ix用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python pandas中的iloc使用小結(jié)
- Pandas中的loc與iloc區(qū)別與用法小結(jié)
- pandas loc與iloc用法及區(qū)別
- python中pandas庫(kù)的iloc函數(shù)用法解析
- 一文秒懂pandas中iloc()函數(shù)
- Pandas庫(kù)中iloc[]函數(shù)的使用方法
- 利用Pandas讀取某列某行數(shù)據(jù)之loc和iloc用法總結(jié)
- Python Pandas數(shù)據(jù)分析之iloc和loc的用法詳解
- python pandas中索引函數(shù)loc和iloc的區(qū)別分析
- pandas中.loc和.iloc以及.at和.iat的區(qū)別說(shuō)明
- pandas中iloc函數(shù)的具體實(shí)現(xiàn)
相關(guān)文章
Python使用Selenium+BeautifulSoup爬取淘寶搜索頁(yè)
這篇文章主要為大家詳細(xì)介紹了Python使用Selenium+BeautifulSoup爬取淘寶搜索頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Python集中化管理平臺(tái)Ansible介紹與YAML簡(jiǎn)介
這篇文章主要介紹了Python集中化管理平臺(tái)Ansible介紹與YAML,簡(jiǎn)單說(shuō)明了集中化管理平臺(tái)Ansible的功能與YAML語(yǔ)言的基本語(yǔ)法與基本使用技巧,需要的朋友可以參考下2019-06-06PyQt5 QTable插入圖片并動(dòng)態(tài)更新的實(shí)例
今天小編就為大家分享一篇PyQt5 QTable插入圖片并動(dòng)態(tài)更新的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06