您的位置: 网站首页> 大家问> 当前文章

single positional indexer is out-of-bounds

老董2022-04-30154围观,129赞

  single positional indexer is out-of-bounds这个报错大家肯定遇见过,一言以蔽之就是你的索引选择超出源数据的索引个数了。有点像列表下标选择中list index out of range!

# -*- coding:UTF-8 -*-

lis = [1,2,3,4]
print(lis[4])
IndexError: list index out of range

  single positional indexer is out-of-bounds这个报错通常在pandas使用中很容易出现。我们来看下:

# -*- coding:UTF-8 -*-
import pandas as pd   

s1 = pd.Series([1,2,3,4])
print(s1.iloc[4])
IndexError: single positional indexer is out-of-bounds
# -*- coding:UTF-8 -*-
import pandas as pd   

df1 = pd.DataFrame({'name':['name1','name2','name3'],'age':[11,16,18]})
print(df1)
print(df1.iloc[3,:])
IndexError: single positional indexer is out-of-bounds

  pandas的DataFrame是二维数据,在选取数据的时候可能是选单个元素,单行多列,多行单列、多行多列等各种情况,pandas数据选取是日常常用的操作,搞定pandas的数据筛选也是熟练掌握pandas的基础,pandas索引器可以是直接索引、iloc索引、loc索引,每种索引方式可以应用切片、布尔索引等。如果想详细了解这些用法,可以查看如下文章:

  >>dateframe loc行列选择,切片,条件筛选大全

  >>dateframe iloc行列选择,切片,条件筛选大全

  >>dateframe []直接索引,切片,条件筛选大全

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    single positional indexer is out-of-bounds文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200