pyspark.pandas.Series.rank ¶
-
Series.
rank
( method : str = 'average' , ascending : bool = True , numeric_only : Optional [ bool ] = None ) → pyspark.pandas.series.Series [source] ¶ -
沿轴计算数值数据的排名(1到n)。相等的值被分配一个排名,该排名是这些值排名的平均值。
注意
当前的rank实现使用了Spark的Window,但没有指定分区规范。这会导致将所有数据移动到单个机器的单个分区中,可能会导致严重的性能下降。避免在大数据集上使用此方法。
- Parameters
-
- method {‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}
-
-
average: 组的平均排名
-
min: 组中的最低排名
-
max: 组中的最高排名
-
first: 按数组中出现的顺序分配排名
-
dense: 类似于‘min’,但在组之间排名总是增加1
-
- ascending boolean, default True
-
从高(1)到低(N)排名的为假
- numeric_only bool, optional
-
如果设置为 True,则对数值型 Series 进行排名,或者对非数值型 Series 返回空 Series
- Returns
-
- ranks same type as caller
示例
>>> s = ps.Series([1, 2, 2, 3], name='A') >>> s 0 1 1 2 2 2 3 3 Name: A, dtype: int64
>>> s.rank() 0 1.0 1 2.5 2 2.5 3 4.0 Name: A, dtype: float64
如果方法设置为‘min’,它使用组中的最低排名。
>>> s.rank(method='min') 0 1.0 1 2.0 2 2.0 3 4.0 Name: A, dtype: float64
如果方法设置为‘max’,它使用组中的最高排名。
>>> s.rank(method='max') 0 1.0 1 3.0 2 3.0 3 4.0 Name: A, dtype: float64
如果方法设置为‘first’,则按顺序分配排名,不进行分组。
>>> s.rank(method='first') 0 1.0 1 2.0 2 3.0 3 4.0 Name: A, dtype: float64
如果方法设置为‘dense’,它不会在组中留下间隙。
>>> s.rank(method='dense') 0 1.0 1 2.0 2 2.0 3 3.0 Name: A, dtype: float64
如果 numeric_only 设置为 ‘True’,则仅对数值型 Series 进行排名,否则返回一个空的 Series。
>>> s = ps.Series(['a', 'b', 'c'], name='A', index=['x', 'y', 'z']) >>> s x a y b z c Name: A, dtype: object
>>> s.rank(numeric_only=True) Series([], Name: A, dtype: float64)