pandas.core.groupby.GroupBy.ngroup#
- final GroupBy.ngroup(ascending=True)[源代码]#
每个组的编号从0到组数-1。
这是Cumcount的枚举补语。请注意,分配给组的数字与迭代Groupby对象时看到组的顺序匹配,而不是与第一次观察到组的顺序匹配。
- 参数
- ascending布尔值,默认为True
如果为False,则反转编号,从组的编号-1到0。
- 退货
- 系列
每个组都有唯一的编号。
参见
cumcount
对每组中的行进行编号。
示例
>>> df = pd.DataFrame({"A": list("aaabba")}) >>> df A 0 a 1 a 2 a 3 b 4 b 5 a >>> df.groupby('A').ngroup() 0 0 1 0 2 0 3 1 4 1 5 0 dtype: int64 >>> df.groupby('A').ngroup(ascending=False) 0 1 1 1 2 1 3 0 4 0 5 1 dtype: int64 >>> df.groupby(["A", [1,1,2,3,2,1]]).ngroup() 0 0 1 0 2 1 3 3 4 2 5 0 dtype: int64