@@ -2370,8 +2370,9 @@ def head(self, n=5):
2370
2370
"""
2371
2371
Return first n rows of each group.
2372
2372
2373
- Essentially equivalent to ``.apply(lambda x: x.head(n))``,
2374
- except ignores as_index flag.
2373
+ Similar to ``.apply(lambda x: x.head(n))``, but it returns a subset of rows
2374
+ from the original DataFrame with original index and order preserved
2375
+ (``as_index`` flag is ignored).
2375
2376
2376
2377
Returns
2377
2378
-------
@@ -2382,10 +2383,6 @@ def head(self, n=5):
2382
2383
2383
2384
>>> df = pd.DataFrame([[1, 2], [1, 4], [5, 6]],
2384
2385
... columns=['A', 'B'])
2385
- >>> df.groupby('A', as_index=False).head(1)
2386
- A B
2387
- 0 1 2
2388
- 2 5 6
2389
2386
>>> df.groupby('A').head(1)
2390
2387
A B
2391
2388
0 1 2
@@ -2401,8 +2398,9 @@ def tail(self, n=5):
2401
2398
"""
2402
2399
Return last n rows of each group.
2403
2400
2404
- Essentially equivalent to ``.apply(lambda x: x.tail(n))``,
2405
- except ignores as_index flag.
2401
+ Similar to ``.apply(lambda x: x.tail(n))``, but it returns a subset of rows
2402
+ from the original DataFrame with original index and order preserved
2403
+ (``as_index`` flag is ignored).
2406
2404
2407
2405
Returns
2408
2406
-------
@@ -2417,10 +2415,6 @@ def tail(self, n=5):
2417
2415
A B
2418
2416
1 a 2
2419
2417
3 b 2
2420
- >>> df.groupby('A').head(1)
2421
- A B
2422
- 0 a 1
2423
- 2 b 1
2424
2418
"""
2425
2419
self ._reset_group_selection ()
2426
2420
mask = self ._cumcount_array (ascending = False ) < n
0 commit comments