pyspark.pandas.Series.rank#
- Series.rank(method='average', ascending=True, numeric_only=False, axis=0, na_option='keep', pct=False)[source]#
Compute numerical data ranks (1 through n) along axis. Equal values are assigned a rank that is the average of the ranks of those values.
Note
the current implementation of rank uses Spark’s Window without specifying partition specification. This leads to moving all data into a single partition in a single machine and could cause serious performance degradation. Avoid this method with very large datasets.
- Parameters:
- method{‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}
average: average rank of group
min: lowest rank in group
max: highest rank in group
first: ranks assigned in order they appear in the array
dense: like ‘min’, but rank always increases by 1 between groups
- ascendingboolean, default True
False for ranks by high (1) to low (N)
- numeric_onlybool, default False
For DataFrame objects, rank only numeric columns if set to True.
Changed in version 4.0.0: The default value of
numeric_onlyis nowFalse.- axis{0 or ‘index’}, default 0
Parameter needed for compatibility with DataFrame.
- na_option{‘keep’, ‘top’, ‘bottom’}, default ‘keep’
keep: leave NA values where they are
top: smallest rank if ascending
bottom: largest rank if ascending
Added in version 4.4.0.
- pctbool, default False
Whether or not to display the returned rankings in percentile form.
Added in version 4.4.0.
- Returns:
- rankssame type as caller
Examples
>>> 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
If method is set to ‘min’, it uses lowest rank in group.
>>> s.rank(method='min') 0 1.0 1 2.0 2 2.0 3 4.0 Name: A, dtype: float64
If method is set to ‘max’, it uses highest rank in group.
>>> s.rank(method='max') 0 1.0 1 3.0 2 3.0 3 4.0 Name: A, dtype: float64
If method is set to ‘first’, it is assigned rank in order without groups.
>>> s.rank(method='first') 0 1.0 1 2.0 2 3.0 3 4.0 Name: A, dtype: float64
If method is set to ‘dense’, it leaves no gaps in group.
>>> s.rank(method='dense') 0 1.0 1 2.0 2 2.0 3 3.0 Name: A, dtype: float64
If numeric_only is set to ‘True’, rank only numeric Series, return an empty Series otherwise.
>>> s = ps.Series(['a', 'b', 'c'], name='A', index=['x', 'y', 'z']) >>> s x a y b z c Name: A, dtype: object
With pct=True, ranks are expressed as percentiles.
>>> s = ps.Series([1, 2, 2, 3], name='A') >>> s.rank(pct=True) 0 0.250 1 0.625 2 0.625 3 1.000 Name: A, dtype: float64
With na_option=’top’, NaN values are assigned the smallest rank.
>>> s = ps.Series([1, float('nan'), 2, 3], name='A') >>> s.rank(na_option='top').sort_index() 0 2.0 1 1.0 2 3.0 3 4.0 Name: A, dtype: float64
With na_option=’bottom’, NaN values are assigned the largest rank.
>>> s.rank(na_option='bottom').sort_index() 0 1.0 1 4.0 2 2.0 3 3.0 Name: A, dtype: float64