pyspark.pandas.Series.iloc ¶
-
property
Series.
iloc
¶ -
纯基于整数位置的索引,用于按位置选择。
.iloc[]
主要是基于整数位置的(从0
到length-1
的轴),但也可以与条件布尔序列一起使用。允许的输入包括:
-
用于列选择的整数,例如
5
。 -
用于行选择的整数列表或数组,具有不同的索引值,例如
[3, 4, 0]
-
用于列选择的整数列表或数组,例如
[4, 3, 0]
。 -
用于列选择的布尔数组。
-
带有整数的切片对象,用于行和列选择,例如
1:7
。
不被允许的输入,而 pandas 允许的是:
-
用于行选择的整数列表或数组,包含重复的索引,例如
[4, 4, 0]
。 -
用于行选择的布尔数组。
-
一个
callable
函数,带有一个参数(调用的 Series、DataFrame 或 Panel),并返回有效的索引输出(上述之一)。这在方法链中非常有用,当你没有引用调用对象但希望基于某些值进行选择时。
.iloc
如果请求的索引器超出范围,将引发IndexError
,除了允许超出范围索引的 切片 索引器(这符合 python/numpy 切片 语义)。另请参阅
-
DataFrame.loc
-
纯标签位置索引器,用于按标签选择。
-
Series.iloc
-
纯基于整数位置的索引,用于按位置选择。
示例
>>> 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 = ps.DataFrame(mydict, columns=['a', 'b', 'c', 'd']) >>> df a b c d 0 1 2 3 4 1 100 200 300 400 2 1000 2000 3000 4000
仅索引行
用于行选择的标量整数。
>>> df.iloc[1] a 100 b 200 c 300 d 400 Name: 1, dtype: int64
>>> df.iloc[[0]] a b c d 0 1 2 3 4
使用一个 切片 对象。
>>> df.iloc[:3] a b c d 0 1 2 3 4 1 100 200 300 400 2 1000 2000 3000 4000
对两个轴进行索引
您可以混合使用索引器类型来选择索引和列。使用
:
来选择整个轴。使用标量整数。
>>> df.iloc[:1, 1] 0 2 Name: b, dtype: int64
使用整数列表。
>>> df.iloc[:2, [1, 3]] b d 0 2 4 1 200 400
使用 切片 对象。
>>> df.iloc[:2, 0:3] a b c 0 1 2 3 1 100 200 300
使用一个布尔数组,其长度与列数匹配。
>>> df.iloc[:, [True, False, True, False]] a c 0 1 3 1 100 300 2 1000 3000
设置值
为所有匹配标签列表的项目设置值。
>>> df.iloc[[1, 2], [1]] = 50 >>> df a b c d 0 1 2 3 4 1 100 50 300 400 2 1000 50 3000 4000
为整行设置值
>>> df.iloc[0] = 10 >>> df a b c d 0 10 10 10 10 1 100 50 300 400 2 1000 50 3000 4000
为整个列设置值
>>> df.iloc[:, 2] = 30 >>> df a b c d 0 10 10 30 10 1 100 50 30 400 2 1000 50 30 4000
为整个列列表设置值
>>> df.iloc[:, [2, 3]] = 100 >>> df a b c d 0 10 10 100 100 1 100 50 100 100 2 1000 50 100 100
使用 Series 设置值
>>> df.iloc[:, 3] = df.iloc[:, 3] * 2 >>> df a b c d 0 10 10 100 200 1 100 50 100 200 2 1000 50 100 200
-