- 用法:- property Series.iloc
- 纯粹基于 integer-location 的索引,用于按位置进行选择。 - .iloc[]主要基于整数位置(从轴的- 0到- length-1),但也可以与布尔数组一起使用。- 允许的输入是: - 一个整数,例如 - 5。
- 整数列表或数组,例如 - [4, 3, 0]。
- 带有整数的切片对象,例如 - 1:7。
- 一个布尔数组。 
- 一个 - callable函数,带有一个参数(调用 Series 或 DataFrame)并返回有效的索引输出(上述之一)。这在方法链中很有用,当您没有对调用对象的引用,但希望您的选择基于某个值时。
 - 如果请求的索引器越界, - .iloc将引发- IndexError,但允许越界索引的切片索引器除外(这符合 python/numpy 切片语义)。- 在按位置选择中查看更多信息。 - 例子:- >>> mydict = [{'a':1, 'b':2, 'c':3, 'd':4}, ... {'a':100, 'b':200, 'c':300, 'd':400}, ... {'a':1000, 'b':2000, 'c':3000, 'd':4000 }] >>> df = pd.DataFrame(mydict) >>> df a b c d 0 1 2 3 4 1 100 200 300 400 2 1000 2000 3000 4000- 仅索引行 - 带有一个标量整数。 - >>> type(df.iloc[0]) <class 'pandas.core.series.Series'> >>> df.iloc[0] a 1 b 2 c 3 d 4 Name:0, dtype:int64- 带有整数列表。 - >>> df.iloc[[0]] a b c d 0 1 2 3 4 >>> type(df.iloc[[0]]) <class 'pandas.core.frame.DataFrame'>- >>> df.iloc[[0, 1]] a b c d 0 1 2 3 4 1 100 200 300 400- 使用 - slice对象。- >>> df.iloc[:3] a b c d 0 1 2 3 4 1 100 200 300 400 2 1000 2000 3000 4000- 使用与索引长度相同的布尔掩码。 - >>> df.iloc[[True, False, True]] a b c d 0 1 2 3 4 2 1000 2000 3000 4000- 带有可调用的,在方法链中很有用。传递给 - lambda的- x是被切片的DataFrame。这将选择索引标签为偶数的行。- >>> df.iloc[lambda x:x.index % 2 == 0] a b c d 0 1 2 3 4 2 1000 2000 3000 4000- 索引两个轴 - 您可以混合索引和列的索引器类型。使用 - :选择整个轴。- 使用标量整数。 - >>> df.iloc[0, 1] 2- 带有整数列表。 - >>> df.iloc[[0, 2], [1, 3]] b d 0 2 4 2 2000 4000- 使用 - slice对象。- >>> df.iloc[1:3, 0:3] a b c 1 100 200 300 2 1000 2000 3000- 使用长度与列匹配的布尔数组。 - >>> df.iloc[:, [True, False, True, False]] a c 0 1 3 1 100 300 2 1000 3000- 使用需要 Series 或 DataFrame 的可调用函数。 - >>> df.iloc[:, lambda df:[0, 2]] a c 0 1 3 1 100 300 2 1000 3000
相关用法
- Python pandas.Series.iat用法及代码示例
- Python pandas.Series.isna用法及代码示例
- Python pandas.Series.iteritems用法及代码示例
- Python pandas.Series.isnull用法及代码示例
- Python pandas.Series.idxmin用法及代码示例
- Python pandas.Series.idxmax用法及代码示例
- Python pandas.Series.interpolate用法及代码示例
- Python pandas.Series.info用法及代码示例
- Python pandas.Series.items用法及代码示例
- Python pandas.Series.isin用法及代码示例
- Python pandas.Series.infer_objects用法及代码示例
- Python pandas.Series.add_prefix用法及代码示例
- Python pandas.Series.map用法及代码示例
- Python pandas.Series.max用法及代码示例
- Python pandas.Series.str.isdecimal用法及代码示例
- Python pandas.Series.str.get用法及代码示例
- Python pandas.Series.to_csv用法及代码示例
- Python pandas.Series.dt.day_name用法及代码示例
- Python pandas.Series.sample用法及代码示例
- Python pandas.Series.head用法及代码示例
注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.Series.iloc。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
