Skip to content

Commit adbdcb1

Browse files
committed
DOC: Adding examples to DataFrameGroupBy.rank pandas-dev#38972
1 parent dad3e7f commit adbdcb1

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

pandas/core/groupby/groupby.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,6 @@ def cumcount(self, ascending: bool = True):
26412641

26422642
@final
26432643
@Substitution(name="groupby")
2644-
@Appender(_common_see_also)
26452644
def rank(
26462645
self,
26472646
method: str = "average",
@@ -2675,6 +2674,49 @@ def rank(
26752674
Returns
26762675
-------
26772676
DataFrame with ranking of values within each group
2677+
2678+
See Also
2679+
--------
2680+
Series.groupby : Apply a function groupby to a Series.
2681+
DataFrame.groupby : Apply a function groupby
2682+
to each row or column of a DataFrame.
2683+
Series.rank : Apply a function rank to a Series.
2684+
DataFrame.rank : Apply a function rank
2685+
to each row or column of a DataFrame.
2686+
2687+
Examples
2688+
--------
2689+
>>> df = pd.DataFrame({'group': ['a', 'a', 'a', 'b', 'a', 'b', 'b', 'b', 'b', 'a'],
2690+
... 'value': [.2, .4, .2, 0.01, .3, .11, .21, .4, .01, 0.2]})
2691+
>>> df
2692+
group value
2693+
0 a 0.20
2694+
1 a 0.40
2695+
2 a 0.20
2696+
3 b 0.01
2697+
4 a 0.30
2698+
5 b 0.11
2699+
6 b 0.21
2700+
7 b 0.40
2701+
8 b 0.01
2702+
9 a 0.20
2703+
>>> df['average_rank'] = df.groupby('group')['value'].rank('average')
2704+
>>> df['min_rank'] = df.groupby('group')['value'].rank('min')
2705+
>>> df['max_rank'] = df.groupby('group')['value'].rank('max')
2706+
>>> df['dense_rank'] = df.groupby('group')['value'].rank('dense')
2707+
>>> df['first_rank'] = df.groupby('group')['value'].rank('first')
2708+
>>> df
2709+
group value average_rank min_rank max_rank dense_rank first_rank
2710+
0 a 0.20 2.0 1.0 3.0 1.0 1.0
2711+
1 a 0.40 5.0 5.0 5.0 3.0 5.0
2712+
2 a 0.20 2.0 1.0 3.0 1.0 2.0
2713+
3 b 0.01 1.5 1.0 2.0 1.0 1.0
2714+
4 a 0.30 4.0 4.0 4.0 2.0 4.0
2715+
5 b 0.11 3.0 3.0 3.0 2.0 3.0
2716+
6 b 0.21 4.0 4.0 4.0 3.0 4.0
2717+
7 b 0.40 5.0 5.0 5.0 4.0 5.0
2718+
8 b 0.01 1.5 1.0 2.0 1.0 2.0
2719+
9 a 0.20 2.0 1.0 3.0 1.0 3.0
26782720
"""
26792721
if na_option not in {"keep", "top", "bottom"}:
26802722
msg = "na_option must be one of 'keep', 'top', or 'bottom'"

0 commit comments

Comments
 (0)