iloc[ ]函数(Pandas库) 您所在的位置:网站首页 copymarkdown什么意思 iloc[ ]函数(Pandas库)

iloc[ ]函数(Pandas库)

#iloc[ ]函数(Pandas库)| 来源: 网络整理| 查看: 265

1 iloc[]函数作用

iloc[]函数,属于pandas库,全称为index location,即对数据进行位置索引,从而在数据表中提取出相应的数据。

2 iloc函数使用

df.iloc[a,b],其中df是DataFrame数据结构的数据(表1就是df),a是行索引(见表1),b是列索引(见表1)。

表1 姓名(列索引10)班级(列索引1)分数(列索引2)0(行索引0)小明302871(行索引1)小王303952(行索引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]



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有