一文秒懂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 4000Pandas庫(kù)中iloc[ ]函數(shù)使用詳解
1 iloc[]函數(shù)作用
iloc[]函數(shù),屬于pandas庫(kù),全稱(chēng)為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是行索引(見(jiàn)表1),b是列索引(見(jiàn)表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:952.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ù)值無(wú)法用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)行索引和切片的方法。它允許你通過(guò)整數(shù)位置來(lái)訪問(wèn) DataFrame 中的特定行和列。
語(yǔ)法格式如下:
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開(kāi)始。這使得 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庫(kù)中iloc[ ]函數(shù)使用詳解的文章就介紹到這了,更多相關(guān)Pandas iloc[ ]函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python Pandas數(shù)據(jù)分析之iloc和loc的用法詳解
- 詳談Pandas中iloc和loc以及ix的區(qū)別
- 聊聊Python pandas 中l(wèi)oc函數(shù)的使用,及跟iloc的區(qū)別說(shuō)明
- python選取特定列 pandas iloc,loc,icol的使用詳解(列切片及行切片)
- 利用Pandas讀取某列某行數(shù)據(jù)之loc和iloc用法總結(jié)
- Pandas中的loc與iloc區(qū)別與用法小結(jié)
- 對(duì)pandas中iloc,loc取數(shù)據(jù)差別及按條件取值的方法詳解
- Python?Pandas中l(wèi)oc和iloc函數(shù)的基本用法示例
- python中pandas庫(kù)的iloc函數(shù)用法解析
- Pandas索引器 loc 和 iloc 比較及代碼示例
相關(guān)文章
python 讀取txt,json和hdf5文件的實(shí)例
今天小編就為大家分享一篇python 讀取txt,json和hdf5文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換
這篇文章主要介紹了python 實(shí)現(xiàn)docx與doc文件的互相轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Pycharm中Python環(huán)境配置常見(jiàn)問(wèn)題解析
這篇文章主要介紹了Pycharm中Python環(huán)境配置常見(jiàn)問(wèn)題,結(jié)合圖文形式分析了Pycharm中Python環(huán)境配置模塊路徑問(wèn)題、虛擬環(huán)境創(chuàng)建、配置遠(yuǎn)程服務(wù)器、連接數(shù)據(jù)庫(kù)等常見(jiàn)問(wèn)題與操作方法,需要的朋友可以參考下2020-01-01
Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型
Python 3最重要的新特性之一是對(duì)字符串和二進(jìn)制數(shù)據(jù)流做了明確的區(qū)分。這篇文章主要介紹了Python3中bytes類(lèi)型轉(zhuǎn)換為str類(lèi)型的相關(guān)知識(shí),需要的朋友可以參考下2018-09-09
Python 中對(duì) XML 文件的編碼轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了Python 中對(duì) XML 文件的編碼轉(zhuǎn)換問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
python3 selenium自動(dòng)化 下拉框定位的例子
今天小編就為大家分享一篇python3 selenium自動(dòng)化 下拉框定位的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python實(shí)現(xiàn)自動(dòng)玩貪吃蛇程序
這篇文章主要介紹了通過(guò)Python實(shí)現(xiàn)的簡(jiǎn)易的自動(dòng)玩貪吃蛇游戲的小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)一學(xué)2022-01-01
Python實(shí)現(xiàn)全自動(dòng)安裝第三方庫(kù)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)全自動(dòng)安裝第三方庫(kù)的方法,一說(shuō)Python要安裝哪個(gè)模塊,我們第一反應(yīng),win+r輸入cmd,pip?instll安裝,但是如果換電腦了,Python第三方庫(kù)那么多,難道要一次一次的敲擊pip?install,本文就介紹一個(gè)簡(jiǎn)單的方法解放雙手,需要的朋友可以參考下2023-07-07

