Polars FAQ

import polars as pl
print('polars:', pl.__version__)
polars: 1.16.0

1 Get the min/max of a column

# sample dataframe
data = {
    'A': [1, 2, None, 4],
    'B': ['a', 'b','c', None], 
    'C': [
            [1, 2],
            [3], 
            [4, 5, 6], 
            None
        ],
    'D': [
            ['a', 'b'],
            ['c'],
            ['d', 'e', 'f'],
            ['g', 'h']
        ],
}

df = pl.DataFrame(data)

print(df)
shape: (4, 4)
┌──────┬──────┬───────────┬─────────────────┐
│ A    ┆ B    ┆ C         ┆ D               │
│ ---  ┆ ---  ┆ ---       ┆ ---             │
│ i64  ┆ str  ┆ list[i64] ┆ list[str]       │
╞══════╪══════╪═══════════╪═════════════════╡
│ 1    ┆ a    ┆ [1, 2]    ┆ ["a", "b"]      │
│ 2    ┆ b    ┆ [3]       ┆ ["c"]           │
│ null ┆ c    ┆ [4, 5, 6] ┆ ["d", "e", "f"] │
│ 4    ┆ null ┆ null      ┆ ["g", "h"]      │
└──────┴──────┴───────────┴─────────────────┘
col_name = 'A'
min_val = df.select(pl.min(col_name)).item()
max_val = df.select(pl.max(col_name)).item()

# print('type of max_val:', type(max_val))
print('min val in col', col_name, ':', min_val)
print('max val in col', col_name, ':', max_val)
min val in col A : 1
max val in col A : 4

method polars.min

Get the min value of each of the specified column(s), separately.


Parameters:

  • name (str) - name(s) of column(s)

method polars.DataFrame.item


Return the DataFrame as a scalar, or return the element at the given row/column.


Parameters:

  • row (int) – row index
  • column (int) – column index

Notes:

  • If row/col not provided, this is equivalent to df[0,0], with a check that the shape is (1,1).
  • With row/col, this is equivalent to df[row,col].
Back to top