polars.DataFrame.row
-
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
indexandby_predicateparams are mutually exclusive. Additionally, to ensure clarity, theby_predicateparameter must be supplied by keyword.When using
by_predicateit 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=Trueto 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_predicateto return the row that matches the given predicate.>>> df.row(by_predicate=(pl.col("ham") == "b")) (2, 7, 'b')
DataFrame.row(
index: int | None = None,
*,
by_predicate: Expr | None = None,
named: bool = False,
) → tuple[Any, ...] | dict[str, Any]
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.row.html