@@ -2326,8 +2326,9 @@ def head(self, n=5):
2326
2326
"""
2327
2327
Return first n rows of each group.
2328
2328
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).
2331
2332
2332
2333
Returns
2333
2334
-------
@@ -2338,10 +2339,6 @@ def head(self, n=5):
2338
2339
2339
2340
>>> df = pd.DataFrame([[1, 2], [1, 4], [5, 6]],
2340
2341
... columns=['A', 'B'])
2341
- >>> df.groupby('A', as_index=False).head(1)
2342
- A B
2343
- 0 1 2
2344
- 2 5 6
2345
2342
>>> df.groupby('A').head(1)
2346
2343
A B
2347
2344
0 1 2
@@ -2357,8 +2354,9 @@ def tail(self, n=5):
2357
2354
"""
2358
2355
Return last n rows of each group.
2359
2356
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).
2362
2360
2363
2361
Returns
2364
2362
-------
@@ -2373,10 +2371,6 @@ def tail(self, n=5):
2373
2371
A B
2374
2372
1 a 2
2375
2373
3 b 2
2376
- >>> df.groupby('A').head(1)
2377
- A B
2378
- 0 a 1
2379
- 2 b 1
2380
2374
"""
2381
2375
self ._reset_group_selection ()
2382
2376
mask = self ._cumcount_array (ascending = False ) < n
0 commit comments