polars.DataFrame.row#
- DataFrame.row( ) tuple[Any, ...] [source]#
- DataFrame.row( ) dict[str, Any]
Get the values of a single row, either by index or by predicate.
- Parameters:
- index
Row index.
- by_predicate
Select the row according to a given expression/predicate.
- named
Return a dictionary instead of a tuple. The dictionary is a mapping of column name to row value. This is more expensive than returning a regular tuple, but allows for accessing values by column name.
- Returns:
- tuple (default) or dictionary of row values
Warning
You should NEVER use this method to iterate over a DataFrame; if you require row-iteration you should strongly prefer use of
iter_rows()
instead.See also
Notes
The
index
andby_predicate
params are mutually exclusive. Additionally, to ensure clarity, theby_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 raisesTooManyRowsReturnedError
, and zero rows will raiseNoRowsReturnedError
(both inherit fromRowsError
).Examples
Specify an index to return the row at the given index as a tuple.
>>> df = pl.DataFrame( ... { ... "foo": [1, 2, 3], ... "bar": [6, 7, 8], ... "ham": ["a", "b", "c"], ... } ... ) >>> df.row(2) (3, 8, 'c')
Specify
named=True
to get a dictionary instead with a mapping of column names to row values.>>> df.row(2, named=True) {'foo': 3, 'bar': 8, 'ham': 'c'}
Use
by_predicate
to return the row that matches the given predicate.>>> df.row(by_predicate=(pl.col("ham") == "b")) (2, 7, 'b')