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的数据。
1
2
3
4
|
import pandas df = pandas.read_csv( 'a.csv' ) print (df.iloc[ 1 , 2 ]) #Out:95 |
2.iloc[a:b,c]:取行索引从a到b-1,列索引为c的数据。注意:在iloc中a:b是左到右不到的,即lioc[1:3,:]是从行索引从1到2,所有列索引的数据。
1
2
3
4
5
6
7
8
|
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的数据。
1
2
3
4
5
6
7
8
9
|
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,所有列索引的数据。
1
2
3
4
5
6
7
8
9
|
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[ ]函数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Fwuyi/article/details/123127754