温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Pandas库中iloc[ ]函数怎么使用

发布时间:2023-04-11 14:54:38 来源:亿速云 阅读:76 作者:iii 栏目:开发技术

本篇内容主要讲解“Pandas库中iloc[ ]函数怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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)小明30287
1(行索引1)小王30395
2(行索引2)小方303100

1.iloc[a,b]:取行索引为a列索引为b的数据。

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,所有列索引的数据。

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[ ]函数怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI