Skip to content

Create an expression representing column(s) in a DataFrame

Description

Create an expression representing column(s) in a DataFrame

Usage

pl$col(...)

Arguments

\<dynamic-dots\> The name or data type of the column(s) to represent. Unnamed objects one of the following:
  • Single string(s) representing column names
    • Regular expressions starting with ^ and ending with $ are allowed.
    • Single wildcard “\*“ has a special meaning: check the examples.
  • Polars DataType(s)

Value

A polars expression

Examples

library("polars")

# a single column by a character
pl$col("foo")
#> col("foo")
# multiple columns by characters
pl$col("foo", "bar")
#> cols(["foo", "bar"])
# multiple columns by polars data types
pl$col(pl$Float64, pl$String)
#> dtype_columns([Float64, String])
# Single `"*"` is converted to a wildcard expression
pl$col("*")
#> *
# Character vectors with length > 1 should be used with `!!!`
pl$col(!!!c("foo", "bar"), "baz")
#> cols(["foo", "bar", "baz"])
pl$col("foo", !!!c("bar", "baz"))
#> cols(["foo", "bar", "baz"])
# there are some special notations for selecting columns
df <- pl$DataFrame(foo = 1:3, bar = 4:6, baz = 7:9)

# select all columns with a wildcard `"*"`
df$select(pl$col("*"))
#> shape: (3, 3)
#> ┌─────┬─────┬─────┐
#> │ foo ┆ bar ┆ baz │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ i32 │
#> ╞═════╪═════╪═════╡
#> │ 1   ┆ 4   ┆ 7   │
#> │ 2   ┆ 5   ┆ 8   │
#> │ 3   ┆ 6   ┆ 9   │
#> └─────┴─────┴─────┘
# select multiple columns by a regular expression
# starts with `^` and ends with `$`
df$select(pl$col("^ba.*$"))
#> shape: (3, 2)
#> ┌─────┬─────┐
#> │ bar ┆ baz │
#> │ --- ┆ --- │
#> │ i32 ┆ i32 │
#> ╞═════╪═════╡
#> │ 4   ┆ 7   │
#> │ 5   ┆ 8   │
#> │ 6   ┆ 9   │
#> └─────┴─────┘