Function col

  • A column in a DataFrame. Can be used to select:

    • a single column by name
    • all columns by using a wildcard "*"
    • column by regular expression if the regex starts with ^ and ends with $

    Parameters

    • col:
          | string
          | DataType
          | string[]
          | pl.Series<any, string>

    Returns pl.Expr

    > df = pl.DataFrame({
    > "ham": [1, 2, 3],
    > "hamburger": [11, 22, 33],
    > "foo": [3, 2, 1]})
    > df.select(col("foo"))
    shape: (3, 1)
    ╭─────╮
    foo
    │ --- │
    i64
    ╞═════╡
    3
    ├╌╌╌╌╌┤
    2
    ├╌╌╌╌╌┤
    1
    ╰─────╯
    > df.select(col("*"))
    shape: (3, 3)
    ╭─────┬───────────┬─────╮
    hamhamburgerfoo
    │ --- ┆ --- ┆ --- │
    i64i64i64
    ╞═════╪═══════════╪═════╡
    1113
    ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    2222
    ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    3331
    ╰─────┴───────────┴─────╯
    > df.select(col("^ham.*$"))
    shape: (3, 2)
    ╭─────┬───────────╮
    hamhamburger
    │ --- ┆ --- │
    i64i64
    ╞═════╪═══════════╡
    111
    ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
    222
    ├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
    333
    ╰─────┴───────────╯
    > df.select(col("*").exclude("ham"))
    shape: (3, 2)
    ╭───────────┬─────╮
    hamburgerfoo
    │ --- ┆ --- │
    i64i64
    ╞═══════════╪═════╡
    113
    ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    222
    ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    331
    ╰───────────┴─────╯
    > df.select(col(["hamburger", "foo"])
    shape: (3, 2)
    ╭───────────┬─────╮
    hamburgerfoo
    │ --- ┆ --- │
    i64i64
    ╞═══════════╪═════╡
    113
    ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    222
    ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    331
    ╰───────────┴─────╯
    > df.select(col(pl.Series(["hamburger", "foo"]))
    shape: (3, 2)
    ╭───────────┬─────╮
    hamburgerfoo
    │ --- ┆ --- │
    i64i64
    ╞═══════════╪═════╡
    113
    ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    222
    ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┤
    331
    ╰───────────┴─────╯