Search code examples
python-polars

How to get a scalar from polars to python without to_series.to_list()[0]


Let's say I want to get the max of a column back to regular python variable

df=pl.DataFrame({'a':[1,2,3]})

Right now I'm doing df.select(pl.col('a').max()).to_series().to_list()[0] but that's seems a bit clunky. Is there a more direct way?


Solution

  • You can index a DataFrame in row, column order.

    >>> df.select(pl.max('a'))[0, 0]
    3
    

    .item() has also since been added.

    >>> df.select(pl.max('a')).item()
    3