maxframe.dataframe.groupby.DataFrameGroupBy.rank#
- DataFrameGroupBy.rank(method='average', ascending=True, na_option='keep', pct=False)#
提供每组内值的排名。
- 参数:
method ({'average', 'min', 'max', 'first', 'dense'}, default 'average') --
average: 组内平均排名。
min: 组内最低排名。
max: 组内最高排名。
first: 按照在数组中出现的顺序分配排名。
dense: 类似'min',但组间排名总是增加1。
ascending (bool, default True) -- False表示从高(1)到低(N)排名。
na_option ({'keep', 'top', 'bottom'}, default 'keep') --
keep: 保留NA值在原位置。
top: 如果升序则为最小排名。
bottom: 如果降序则为最小排名。
pct (bool, default False) -- 计算每组内数据的百分比排名。
- 返回类型:
DataFrame with ranking of values within each group
参见
Series.groupby对Series应用groupby函数。
DataFrame.groupby对DataFrame的每一行或每一列应用groupby函数。
示例
>>> import maxframe.dataframe as md >>> df = md.DataFrame( ... { ... "group": ["a", "a", "a", "a", "a", "b", "b", "b", "b", "b"], ... "value": [2, 4, 2, 3, 5, 1, 2, 4, 1, 5], ... } ... ) >>> df.execute() group value 0 a 2 1 a 4 2 a 2 3 a 3 4 a 5 5 b 1 6 b 2 7 b 4 8 b 1 9 b 5 >>> for method in ['average', 'min', 'max', 'dense', 'first']: ... df[f'{method}_rank'] = df.groupby('group')['value'].rank(method) >>> df.execute() group value average_rank min_rank max_rank dense_rank first_rank 0 a 2 1.5 1.0 2.0 1.0 1.0 1 a 4 4.0 4.0 4.0 3.0 4.0 2 a 2 1.5 1.0 2.0 1.0 2.0 3 a 3 3.0 3.0 3.0 2.0 3.0 4 a 5 5.0 5.0 5.0 4.0 5.0 5 b 1 1.5 1.0 2.0 1.0 1.0 6 b 2 3.0 3.0 3.0 2.0 3.0 7 b 4 4.0 4.0 4.0 3.0 4.0 8 b 1 1.5 1.0 2.0 1.0 2.0 9 b 5 5.0 5.0 5.0 4.0 5.0