pandas 1.4.2

ParametersReturns
rank(self: 'NDFrameT', axis=0, method: 'str' = 'average', numeric_only: 'bool_t | None | lib.NoDefault' = <no_default>, na_option: 'str' = 'keep', ascending: 'bool_t' = True, pct: 'bool_t' = False) -> 'NDFrameT'

By default, equal values are assigned a rank that is the average of the ranks of those values.

Parameters

axis : {0 or 'index', 1 or 'columns'}, default 0

Index to direct ranking.

method : {'average', 'min', 'max', 'first', 'dense'}, default 'average'

How to rank the group of records that have the same value (i.e. ties):

  • average: average rank of the group

  • min: lowest rank in the group

  • max: highest rank in the group

  • first: ranks assigned in order they appear in the array

  • dense: like 'min', but rank always increases by 1 between groups.

numeric_only : bool, optional

For DataFrame objects, rank only numeric columns if set to True.

na_option : {'keep', 'top', 'bottom'}, default 'keep'

How to rank NaN values:

  • keep: assign NaN rank to NaN values

  • top: assign lowest rank to NaN values

  • bottom: assign highest rank to NaN values

ascending : bool, default True

Whether or not the elements should be ranked in ascending order.

pct : bool, default False

Whether or not to display the returned rankings in percentile form.

Returns

same type as caller

Return a Series or DataFrame with data ranks as values.

Compute numerical data ranks (1 through n) along axis.

See Also

core.groupby.GroupBy.rank

Rank of values within each group.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame(data={'Animal': ['cat', 'penguin', 'dog',
...  'spider', 'snake'],
...  'Number_legs': [4, 2, 4, 8, np.nan]})
... df Animal Number_legs 0 cat 4.0 1 penguin 2.0 2 dog 4.0 3 spider 8.0 4 snake NaN

The following example shows how the method behaves with the above parameters:

This example is valid syntax, but we were not able to check execution
>>> df['default_rank'] = df['Number_legs'].rank()
... df['max_rank'] = df['Number_legs'].rank(method='max')
... df['NA_bottom'] = df['Number_legs'].rank(na_option='bottom')
... df['pct_rank'] = df['Number_legs'].rank(pct=True)
... df Animal Number_legs default_rank max_rank NA_bottom pct_rank 0 cat 4.0 2.5 3.0 2.5 0.625 1 penguin 2.0 1.0 1.0 1.0 0.250 2 dog 4.0 2.5 3.0 2.5 0.625 3 spider 8.0 4.0 4.0 4.0 1.000 4 snake NaN NaN NaN 5.0 NaN
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#8459
type: <class 'function'>
Commit: