Skip to content

Create an expression representing column(s) in a DataFrame

Source code

Description

Create an expression representing column(s) in a DataFrame

Usage

pl$col(names)

Arguments

names The name(s) or data type of the column(s) to represent. It accepts one of the following:
  • A character vector representing column names
    • Regular expressions starting with ^ and ending with $ are allowed.
    • Single wildcard “\*“ has a special meaning: check the examples.
  • A Polars DataType or list of Polars data types

Value

A polars expression

Examples

library("polars")

# a single column by a character
pl$col("foo")
#> col("foo")
# multiple columns by characters
pl$col(c("foo", "bar"))
#> cs.by_name('foo', 'bar', require_all=true)
# multiple columns by polars data types
pl$col(c(pl$Float64, pl$String))
#> cs.by_dtype([Float64, String])
# Single `"*"` is converted to a wildcard expression
pl$col("*")
#> cs.all()
# 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   │
#> └─────┴─────┘