Skip to content

Commit 76439de

Browse files
committed
fixes #6721 GroupBy.head()/tail() documentation
1 parent c0ff67a commit 76439de

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

pandas/core/groupby/groupby.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -2326,8 +2326,9 @@ def head(self, n=5):
23262326
"""
23272327
Return first n rows of each group.
23282328
2329-
Essentially equivalent to ``.apply(lambda x: x.head(n))``,
2330-
except ignores as_index flag.
2329+
Similar to ``.apply(lambda x: x.head(n))``, but it returns a subset of rows
2330+
from the original DataFrame with original index and order preserved (``as_index``
2331+
flag is ignored).
23312332
23322333
Returns
23332334
-------
@@ -2338,10 +2339,6 @@ def head(self, n=5):
23382339
23392340
>>> df = pd.DataFrame([[1, 2], [1, 4], [5, 6]],
23402341
... columns=['A', 'B'])
2341-
>>> df.groupby('A', as_index=False).head(1)
2342-
A B
2343-
0 1 2
2344-
2 5 6
23452342
>>> df.groupby('A').head(1)
23462343
A B
23472344
0 1 2
@@ -2357,8 +2354,9 @@ def tail(self, n=5):
23572354
"""
23582355
Return last n rows of each group.
23592356
2360-
Essentially equivalent to ``.apply(lambda x: x.tail(n))``,
2361-
except ignores as_index flag.
2357+
Similar to ``.apply(lambda x: x.tail(n))``, but it returns a subset of rows
2358+
from the original DataFrame with original index and order preserved (``as_index``
2359+
flag is ignored).
23622360
23632361
Returns
23642362
-------
@@ -2373,10 +2371,6 @@ def tail(self, n=5):
23732371
A B
23742372
1 a 2
23752373
3 b 2
2376-
>>> df.groupby('A').head(1)
2377-
A B
2378-
0 a 1
2379-
2 b 1
23802374
"""
23812375
self._reset_group_selection()
23822376
mask = self._cumcount_array(ascending=False) < n

0 commit comments

Comments
 (0)