-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Add groupby().ngroup() method to count groups (#11642) #14026
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,7 +150,7 @@ | |
'last', 'first', | ||
'head', 'tail', 'median', | ||
'mean', 'sum', 'min', 'max', | ||
'cumcount', | ||
'cumcount', 'ngroup', | ||
'resample', | ||
'rank', 'quantile', | ||
'fillna', | ||
|
@@ -1437,6 +1437,75 @@ def nth(self, n, dropna=None): | |
|
||
return result | ||
|
||
@Substitution(name='groupby') | ||
@Appender(_doc_template) | ||
def ngroup(self, ascending=True): | ||
""" | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment is good, add something like this to the docs (in groupy.rst) where you show an example |
||
|
||
.. versionadded:: 0.20.2 | ||
|
||
Parameters | ||
---------- | ||
ascending : bool, default True | ||
If False, number in reverse, from number of group - 1 to 0. | ||
|
||
Examples | ||
-------- | ||
|
||
>>> 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 | ||
|
||
See also | ||
-------- | ||
.cumcount : Number the rows in each group. | ||
|
||
""" | ||
|
||
self._set_group_selection() | ||
|
||
index = self._selected_obj.index | ||
result = Series(self.grouper.group_info[0], index) | ||
if not ascending: | ||
result = self.ngroups - 1 - result | ||
return result | ||
|
||
@Substitution(name='groupby') | ||
@Appender(_doc_template) | ||
def cumcount(self, ascending=True): | ||
|
@@ -1481,6 +1550,10 @@ def cumcount(self, ascending=True): | |
4 0 | ||
5 0 | ||
dtype: int64 | ||
|
||
See also | ||
-------- | ||
.ngroup : Number the groups themselves. | ||
""" | ||
|
||
self._set_group_selection() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versionadded tag