一文秒懂pandas中iloc()函數(shù)
pandas中iloc()函數(shù)
DataFrame.iloc
純基于整數(shù)位置的索引。
import pandas as pd mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 100, 'b': 200, 'c': 300, 'd': 400}, {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }] '''mydict [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 100, 'b': 200, 'c': 300, 'd': 400}, {'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000}]''' df = pd.DataFrame(mydict) ''' df a b c d 0 1 2 3 4 1 100 200 300 400 2 1000 2000 3000 4000 '''
df.iloc[0]#取第0行 a 1 b 2 c 3 d 4 Name: 0, dtype: int64 df.iloc[0].shape (4,) type(df.iloc[0].shape) tuple df.iloc[[0]] a b c d 0 1 2 3 4 type(df.iloc[[0]]) pandas.core.frame.DataFrame df.iloc[[0,2]]#取第0、2行 a b c d 0 1 2 3 4 2 1000 2000 3000 4000 df.iloc[0:2,0:3]#取0到1行和0到2列 a b c 0 1 2 3 1 100 200 300 df.iloc[[True, False, True]]#不常用 a b c d 0 1 2 3 4 2 1000 2000 3000 4000 df.iloc[lambda x: x.index % 2 == 0]#函數(shù)生成索引列表,x即df a b c d 0 1 2 3 4 2 1000 2000 3000 4000
Pandas庫中iloc[ ]函數(shù)使用詳解
1 iloc[]函數(shù)作用
iloc[]函數(shù),屬于pandas庫,全稱為index location,即對(duì)數(shù)據(jù)進(jìn)行位置索引,從而在數(shù)據(jù)表中提取出相應(yīng)的數(shù)據(jù)。
2 iloc函數(shù)使用
df.iloc[a,b],其中df是DataFrame數(shù)據(jù)結(jié)構(gòu)的數(shù)據(jù)(表1就是df),a是行索引(見表1),b是列索引(見表1)。
姓名(列索引10) | 班級(jí)(列索引1) | 分?jǐn)?shù)(列索引2) | |
0(行索引0) | 小明 | 302 | 87 |
1(行索引1) | 小王 | 303 | 95 |
2(行索引2) | 小方 | 303 | 100 |
1.iloc[a,b]:取行索引為a列索引為b的數(shù)據(jù)。
import pandas df = pandas.read_csv('a.csv') print(df.iloc[1,2]) #Out:95
2.iloc[a:b,c]:取行索引從a到b-1,列索引為c的數(shù)據(jù)。注意:在iloc中a:b是左到右不到的,即lioc[1:3,:]是從行索引從1到2,所有列索引的數(shù)據(jù)。
import pandas df = pandas.read_csv('a.csv') print(df.iloc[0:2,2]) #數(shù)據(jù)結(jié)構(gòu)是Series print(df.iloc[0:2,2].values) #數(shù)據(jù)結(jié)構(gòu)是ndarray #Out1:0 87 # 1 95 # Name: 分?jǐn)?shù), dtype: int64 #Out2:[87 95]
iloc[].values,用values屬性取值,返回ndarray,但是單個(gè)數(shù)值無法用values函數(shù)讀取。
3.iloc[a:b,c:d]:取行索引從a到b-1,列索引從c到d-1的數(shù)據(jù)。
import pandas df = pandas.read_csv('a.csv') print(df.iloc[0:2,0:2]) print(df.iloc[0:2,0:2].values) #Out1: 姓名 班級(jí) # 0 小明 302 # 1 小王 303 #Out2:[['小明' 302] # ['小王' 303]]
4.iloc[a]:取取行索引為a,所有列索引的數(shù)據(jù)。
import pandas df = pandas.read_csv('a.csv') print(df.iloc[2]) print(df.iloc[2].values) #Out1:姓名 小方 # 班級(jí) 303 # 分?jǐn)?shù) 100 # Name: 2, dtype: object #Out2:['小方' 303 100]
補(bǔ)充:pandas的iloc函數(shù)
pandas的iloc函數(shù):
iloc
是 Pandas 中用于基于整數(shù)位置進(jìn)行索引和切片的方法。它允許你通過整數(shù)位置來訪問 DataFrame 中的特定行和列。
語法格式如下:
DataFrame.iloc[row_indexer, column_indexer]
row_indexer
: 行的整數(shù)位置或切片。column_indexer
: 列的整數(shù)位置或切片。
下面是一些使用 iloc
的示例:
import pandas as pd # 創(chuàng)建一個(gè)示例 DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, 30, 35, 40], 'City': ['New York', 'San Francisco', 'Los Angeles', 'Chicago']} df = pd.DataFrame(data) # 使用 iloc 獲取特定行和列的數(shù)據(jù) # 獲取第二行(索引為1)的所有列數(shù)據(jù) row_1 = df.iloc[1, :] # 獲取第一列(索引為0)的所有行數(shù)據(jù) column_0 = df.iloc[:, 0] # 獲取第二行到第四行(索引為1到3)的第一列和第二列的數(shù)據(jù) subset = df.iloc[1:4, 0:2] print("Row 1:") print(row_1) print("\nColumn 0:") print(column_0) print("\nSubset:") print(subset)
在這個(gè)例子中,iloc
被用于獲取指定的行和列。要注意,iloc
使用的是整數(shù)位置,而不是標(biāo)簽。索引從0開始。這使得 iloc
適用于對(duì) DataFrame 進(jìn)行基于位置的切片和索引。
Row 1: Name Bob Age 30 City San Francisco Name: 1, dtype: object Column 0: 0 Alice 1 Bob 2 Charlie 3 David Name: Name, dtype: object Subset: Name Age 1 Bob 30 2 Charlie 35 3 David 40
到此這篇關(guān)于Pandas庫中iloc[ ]函數(shù)使用詳解的文章就介紹到這了,更多相關(guān)Pandas iloc[ ]函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)讀取大量Excel文件并跨文件批量計(jì)算平均值
這篇文章主要為大家詳細(xì)介紹了如何利用Python語言,實(shí)現(xiàn)對(duì)多個(gè)不同Excel文件進(jìn)行數(shù)據(jù)讀取與平均值計(jì)算的方法,感興趣的可以了解一下2023-02-02Python使用numpy模塊創(chuàng)建數(shù)組操作示例
這篇文章主要介紹了Python使用numpy模塊創(chuàng)建數(shù)組操作,結(jié)合實(shí)例形式分析了Python使用numpy模塊實(shí)現(xiàn)數(shù)組的創(chuàng)建、賦值、修改、打印等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06python實(shí)現(xiàn)簡單點(diǎn)對(duì)點(diǎn)(p2p)聊天
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單點(diǎn)對(duì)點(diǎn)p2p聊天,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09python支持?jǐn)帱c(diǎn)續(xù)傳的多線程下載示例
這篇文章主要介紹了python支持?jǐn)帱c(diǎn)續(xù)傳的多線程下載示例,大家參考使用吧2014-01-01詳解Open Folder as PyCharm Project怎么添加的方法
這篇文章主要介紹了詳解Open Folder as PyCharm Project怎么添加的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12淺談Pytorch中的自動(dòng)求導(dǎo)函數(shù)backward()所需參數(shù)的含義
今天小編就為大家分享一篇淺談Pytorch中的自動(dòng)求導(dǎo)函數(shù)backward()所需參數(shù)的含義,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python隨機(jī)生成手機(jī)號(hào)、數(shù)字的方法詳解
這篇文章主要介紹了Python隨機(jī)生成手機(jī)號(hào)、數(shù)字的方法,結(jié)合完整實(shí)例形式分析了Python編程生成隨機(jī)手機(jī)號(hào)與數(shù)字的實(shí)現(xiàn)方法及相關(guān)函數(shù)用法,需要的朋友可以參考下2017-07-07200行python代碼實(shí)現(xiàn)2048游戲
這篇文章主要為大家詳細(xì)介紹了200行Python代碼實(shí)現(xiàn)2048游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07