polars.DataFrame.row#

DataFrame.row(index: Optional[int] = None, *, by_predicate: Optional[Expr] = None) tuple[Any, ...][source]#

Get a row as tuple, either by index or by predicate.

Parameters:
index

Row index.

by_predicate

Select the row according to a given expression/predicate.

Notes

The index and by_predicate params are mutually exclusive. Additionally, to ensure clarity, the by_predicate parameter must be supplied by keyword.

When using by_predicate it is an error condition if anything other than one row is returned; more than one row raises TooManyRowsReturned, and zero rows will raise NoRowsReturned (both inherit from RowsException).

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...         "ham": ["a", "b", "c"],
...     }
... )
>>> # return the row at the given index
>>> df.row(2)
(3, 8, 'c')
>>> # return the row that matches the given predicate
>>> df.row(by_predicate=(pl.col("ham") == "b"))
(2, 7, 'b')