pandas 1.4.2

ParametersReturnsBackRef
nth(self, n: 'PositionalIndexer | tuple', dropna: "Literal['any', 'all', None]" = None) -> 'NDFrameT'

Can be either a call or an index. dropna is not available with index notation. Index notation accepts a comma separated list of integers and slices.

If dropna, will take the nth non-null row, dropna is either 'all' or 'any'; this is equivalent to calling dropna(how=dropna) before the groupby.

Parameters

n : int, slice or list of ints and slices

A single nth value for the row or a list of nth values or slices.

versionchanged

Added slice and lists containing slices. Added index notation.

dropna : {'any', 'all', None}, default None

Apply the specified dropna operation before counting which row is the nth row. Only supported if n is an int.

Returns

Series or DataFrame

N-th value within each group.

Take the nth row from each group if n is an int, otherwise a subset of rows.

See Also

DataFrame.groupby

Apply a function groupby to each row or column of a DataFrame.

Series.groupby

Apply a function groupby to a Series.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> g.nth(1)
     B
A
1  2.0
2  5.0
This example is valid syntax, but we were not able to check execution
>>> g.nth(-1)
     B
A
1  4.0
2  5.0
This example is valid syntax, but we were not able to check execution
>>> g.nth([0, 1])
     B
A
1  NaN
1  2.0
2  3.0
2  5.0
This example is valid syntax, but we were not able to check execution
>>> g.nth(slice(None, -1))
     B
A
1  NaN
1  2.0
2  3.0

Index notation may also be used

This example is valid syntax, but we were not able to check execution
>>> g.nth[0, 1]
     B
A
1  NaN
1  2.0
2  3.0
2  5.0
This example is valid syntax, but we were not able to check execution
>>> g.nth[:-1]
     B
A
1  NaN
1  2.0
2  3.0

Specifying :None:None:`dropna` allows count ignoring NaN

This example is valid syntax, but we were not able to check execution
>>> g.nth(0, dropna='any')
     B
A
1  2.0
2  3.0

NaNs denote group exhausted when using dropna

This example is valid syntax, but we were not able to check execution
>>> g.nth(3, dropna='any')
    B
A
1 NaN
2 NaN

Specifying :None:None:`as_index=False` in groupby keeps the original index.

This example is valid syntax, but we were not able to check execution
>>> df.groupby('A', as_index=False).nth(1)
   A    B
1  1  2.0
4  2  5.0
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.groupby.indexing.GroupByPositionalSelector pandas.core.groupby.indexing.GroupByPositionalSelector.__getitem__

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/groupby/groupby.py#2622
type: <class 'function'>
Commit: