polars.DataFrame.iter_rows#

DataFrame.iter_rows(named: Literal[False] = False, buffer_size: int = 500) Iterator[tuple[Any, ...]][source]#
DataFrame.iter_rows(named: Literal[True], buffer_size: int = 500) Iterator[dict[str, Any]]

Returns an iterator over the DataFrame of rows of python-native values.

Parameters:
named

Return dictionaries instead of tuples. The dictionaries are 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.

buffer_size

Determines the number of rows that are buffered internally while iterating over the data; you should only modify this in very specific cases where the default value is determined not to be a good fit to your access pattern, as the speedup from using the buffer is significant (~2-4x). Setting this value to zero disables row buffering.

Returns:
An iterator of tuples (default) or dictionaries of python row values.

Warning

Row iteration is not optimal as the underlying data is stored in columnar form; where possible, prefer export via one of the dedicated export/output methods.

See also

rows

Materialises all frame data as a list of rows.

Notes

If you have ns-precision temporal values you should be aware that python natively only supports up to us-precision; if this matters you should export to a different format.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 3, 5],
...         "b": [2, 4, 6],
...     }
... )
>>> [row[0] for row in df.iter_rows()]
[1, 3, 5]
>>> [row["b"] for row in df.iter_rows(named=True)]
[2, 4, 6]