-
GroupBy.nth(n, dropna=None)
[source] -
Take the nth row from each group if n is an int, or a subset of rows if n is a list of ints.
If dropna, will take the nth non-null row, dropna is either Truthy (if a Series) or ?all?, ?any? (if a DataFrame); this is equivalent to calling dropna(how=dropna) before the groupby.
Parameters: n : int or list of ints
a single nth value for the row or a list of nth values
dropna : None or str, optional
apply the specified dropna operation before counting which row is the nth row. Needs to be None, ?any? or ?all?
Examples
12345678910111213141516171819202122232425>>> df
=
pd.DataFrame({
'A'
: [
1
,
1
,
2
,
1
,
2
],
...
'B'
: [np.nan,
2
,
3
,
4
,
5
]}, columns
=
[
'A'
,
'B'
])
>>> g
=
df.groupby(
'A'
)
>>> g.nth(
0
)
B
A
1
NaN
2
3.0
>>> g.nth(
1
)
B
A
1
2.0
2
5.0
>>> g.nth(
-
1
)
B
A
1
4.0
2
5.0
>>> g.nth([
0
,
1
])
B
A
1
NaN
1
2.0
2
3.0
2
5.0
Specifying
dropna
allows count ignoring NaN12345>>> g.nth(
0
, dropna
=
'any'
)
B
A
1
2.0
2
3.0
NaNs denote group exhausted when using dropna
12345>>> g.nth(
3
, dropna
=
'any'
)
B
A
1
NaN
2
NaN
Specifying
as_index=False
ingroupby
keeps the original index.1234>>> df.groupby(
'A'
, as_index
=
False
).nth(
1
)
A B
1
1
2.0
4
2
5.0
GroupBy.nth()

2025-01-10 15:47:30
Please login to continue.