Pandas庫中iloc[]函數的使用方法
1 iloc[]函數作用
iloc[]函數,屬于pandas庫,全稱為index location,即對數據進行位置索引,從而在數據表中提取出相應的數據。
2 iloc函數使用
df.iloc[a,b],其中df是DataFrame數據結構的數據(表1就是df),a是行索引(見表1),b是列索引(見表1)。
| 姓名(列索引10) | 班級(列索引1) | 分數(列索引2) | |
| 0(行索引0) | 小明 | 302 | 87 |
| 1(行索引1) | 小王 | 303 | 95 |
| 2(行索引2) | 小方 | 303 | 100 |
1.iloc[a,b]:取行索引為a列索引為b的數據。
import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[1,2])
#Out:952.iloc[a:b,c]:取行索引從a到b-1,列索引為c的數據。注意:在iloc中a:b是左到右不到的,即lioc[1:3,:]是從行索引從1到2,所有列索引的數據。
import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[0:2,2]) #數據結構是Series
print(df.iloc[0:2,2].values) #數據結構是ndarray
#Out1:0 87
# 1 95
# Name: 分數, dtype: int64
#Out2:[87 95]iloc[].values,用values屬性取值,返回ndarray,但是單個數值無法用values函數讀取。
3.iloc[a:b,c:d]:取行索引從a到b-1,列索引從c到d-1的數據。
import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[0:2,0:2])
print(df.iloc[0:2,0:2].values)
#Out1: 姓名 班級
# 0 小明 302
# 1 小王 303
#Out2:[['小明' 302]
# ['小王' 303]]4.iloc[a]:取取行索引為a,所有列索引的數據。
import pandas
df = pandas.read_csv('a.csv')
print(df.iloc[2])
print(df.iloc[2].values)
#Out1:姓名 小方
# 班級 303
# 分數 100
# Name: 2, dtype: object
#Out2:['小方' 303 100]總結
到此這篇關于Pandas庫中iloc[]函數使用的文章就介紹到這了,更多相關Pandas庫iloc[]函數使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python中not not x 與bool(x) 的區(qū)別
這篇文章主要介紹了python中not not x 與 bool(x) 的區(qū)別,我們就來做一個選擇,就是 not not x 和 bool(x) 用哪個比較好?下面一起進入文章看看吧2021-12-12
Pytorch如何打印與Keras的model.summary()類似的輸出(最新推薦)
這篇文章主要介紹了Pytorch如何打印與Keras的model.summary()類似的輸出,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

