You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/user_guide/groupby.rst
+24-14
Original file line number
Diff line number
Diff line change
@@ -1354,9 +1354,14 @@ This shows the first or last n rows from each group.
1354
1354
Taking the nth row of each group
1355
1355
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1356
1356
1357
-
To select from a DataFrame or Series the nth item, use
1358
-
:meth:`~pd.core.groupby.DataFrameGroupBy.nth`. This is a reduction method, and
1359
-
will return a single row (or no row) per group if you pass an int for n:
1357
+
To select the nth item from each group, use :meth:`.DataFrameGroupBy.nth` or
1358
+
:meth:`.SeriesGroupBy.nth`. Arguments supplied can be any integer, lists of integers,
1359
+
slices, or lists of slices; see below for examples. When the nth element of a group
1360
+
does not exist an error is *not* raised; instead no corresponding rows are returned.
1361
+
1362
+
In general this operation acts as a filtration. In certain cases it will also return
1363
+
one row per group, making it also a reduction. However because in general it can
1364
+
return zero or multiple rows per group, pandas treats it as a filtration in all cases.
1360
1365
1361
1366
.. ipython:: python
1362
1367
@@ -1367,6 +1372,14 @@ will return a single row (or no row) per group if you pass an int for n:
1367
1372
g.nth(-1)
1368
1373
g.nth(1)
1369
1374
1375
+
If the nth element of a group does not exist, then no corresponding row is included
1376
+
in the result. In particular, if the specified ``n`` is larger than any group, the
1377
+
result will be an empty DataFrame.
1378
+
1379
+
.. ipython:: python
1380
+
1381
+
g.nth(5)
1382
+
1370
1383
If you want to select the nth not-null item, use the ``dropna`` kwarg. For a DataFrame this should be either ``'any'`` or ``'all'`` just like you would pass to dropna:
1371
1384
1372
1385
.. ipython:: python
@@ -1376,21 +1389,11 @@ If you want to select the nth not-null item, use the ``dropna`` kwarg. For a Dat
1376
1389
g.first()
1377
1390
1378
1391
# nth(-1) is the same as g.last()
1379
-
g.nth(-1, dropna="any")# NaNs denote group exhausted when using dropna
1392
+
g.nth(-1, dropna="any")
1380
1393
g.last()
1381
1394
1382
1395
g.B.nth(0, dropna="all")
1383
1396
1384
-
As with other methods, passing ``as_index=False``, will achieve a filtration, which returns the grouped row.
0 commit comments