中国统计网|Python拍了拍你,一份【同时选择行列方法】的盘点送给你( 二 )

-- 输出报错: #报错:英:IndexError: .iloc requires numeric indexers, got ['name''age'];汉:iloc需要数字索引器 , 得到['name''age'] #原因分析:当使用行(列)名称索引时 , 不能使用iloc方法;若使用iloc , 必须要求行或列都严格使用数字或者切片进行索引; #报错:英:TypeError: cannotdoslice indexing on <class'pandas.core.indexes.base.Index'> with these indexers [0] of <class'int'> / 中:TypeError:无法使用<class'int'>的这些索引器[0]在<class>上进行切片索引 #报错:英:AttributeError:“ DataFrame”对象没有属性“ ix”;/汉:AttributeError:“ DataFrame”对象没有属性“ ix” #原因分析:新版本的anconda目前已经不支持"ix"了!方式四位置索引 + 位置索引选择指定行和列# 位置索引:选择第1,4行 , 第1,3列 s.iloc[[0,3],[0,2]] --输出结果: nameage A1qinlu 18 A4junjun 25 # case1:索引超出 s.iloc[[1,2],[2,3]] # 报错:IndexError: positional indexers are out-of-bounds/汉:IndexError:位置索引器超出范围; # case2:s.loc[[0,3],[0,2]] # 报错:KeyError: "None of [Int64Index([0, 3], dtype='int64')] are in the [index]"/ 中文:KeyError:“ [Int64Index([0 , 3] , dtype ='int64')]都不在[索引]中” # 原因分析:当使用位置索引 + 位置索引选择指定行和列时 , 需要利用iloc方法传入行列位置方式五普通索引 + 普通索引选择指定行和列# 普通索引 + 普通索引就是通过同时传入行和列的索引名称进行数据选择 , 需要用到loc方法; s.loc[['A1','A2','A4'],['name','age']] -- 输出结果: name age A1 qinlu18 A2 lulu19 A4 junjun 25 # 报错:s.iloc[['A1','A2','A4'],['name','age']];英文:IndexError: .iloc requires numeric indexers, got ['A1''A2''A4']End.
作者:爱数据小辉
本文为中国统计网原创 , 转载请联系后台


推荐阅读