maxframe.dataframe.groupby.GroupBy.cumcount#

GroupBy.cumcount(ascending: bool = True)#

对每个组中的每一项从 0 到该组长度减一进行编号。

本质上这等价于

self.apply(lambda x: pd.Series(np.arange(len(x)), x.index))
参数:

ascending (bool, default True) -- 如果为 False,则反向编号,从组长度减一到 0。

返回:

每个组内各个元素的序号。

返回类型:

Series

参见

ngroup

对组本身进行编号。

示例

>>> import maxframe.tensor as mt
>>> import maxframe.dataframe as md
>>> df = md.DataFrame([['a'], ['a'], ['a'], ['b'], ['b'], ['a']],
...                   columns=['A'])
>>> df.execute()
   A
0  a
1  a
2  a
3  b
4  b
5  a
>>> df.groupby('A').cumcount().execute()
0    0
1    1
2    2
3    0
4    1
5    3
dtype: int64
>>> df.groupby('A').cumcount(ascending=False).execute()
0    3
1    2
2    1
3    1
4    0
5    0
dtype: int64