pandas.core.groupby.DataFrameGroupBy.ngroup#
- DataFrameGroupBy.ngroup(ascending=True)[source]#
Number each group from 0 to the number of groups - 1.
This is the enumerative complement of cumcount. Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed.
Groups with missing keys (where pd.isna() is True) will be labeled with NaN and will be skipped from the count.
- Parameters
- ascendingbool, default True
If False, number in reverse, from number of group - 1 to 0.
- Returns
- Series
Unique numbers for each group.
See also
cumcount
Number the rows in each group.
Examples
>>> df = pd.DataFrame({"color": ["red", None, "red", "blue", "blue", "red"]}) >>> df color 0 red 1 None 2 red 3 blue 4 blue 5 red >>> df.groupby("color").ngroup() 0 1.0 1 NaN 2 1.0 3 0.0 4 0.0 5 1.0 dtype: float64 >>> df.groupby("color", dropna=False).ngroup() 0 1 1 2 2 1 3 0 4 0 5 1 dtype: int64 >>> df.groupby("color", dropna=False).ngroup(ascending=False) 0 1 1 0 2 1 3 2 4 2 5 1 dtype: int64