Method: Polars::DataFrame#with_columns
- Defined in:
- lib/polars/data_frame.rb
#with_columns(*exprs, **named_exprs) ⇒ DataFrame
Add columns to this DataFrame.
Added columns will replace existing columns with the same name.
Examples:
Pass an expression to add it as a new column.
Pass an expression to add it as a new column.
df = Polars::DataFrame.new(
{
"a" => [1, 2, 3, 4],
"b" => [0.5, 4, 10, 13],
"c" => [true, true, false, true]
}
)
df.with_columns((Polars.col("a") ** 2).alias("a^2"))
# =>
# shape: (4, 4)
# ┌─────┬──────┬───────┬─────┐
# │ a ┆ b ┆ c ┆ a^2 │
# │ --- ┆ --- ┆ --- ┆ --- │
# │ i64 ┆ f64 ┆ bool ┆ i64 │
# ╞═════╪══════╪═══════╪═════╡
# │ 1 ┆ 0.5 ┆ true ┆ 1 │
# │ 2 ┆ 4.0 ┆ true ┆ 4 │
# │ 3 ┆ 10.0 ┆ false ┆ 9 │
# │ 4 ┆ 13.0 ┆ true ┆ 16 │
# └─────┴──────┴───────┴─────┘
Added columns will replace existing columns with the same name.
Added columns will replace existing columns with the same name.
df.with_columns(Polars.col("a").cast(Polars::Float64))
# =>
# shape: (4, 3)
# ┌─────┬──────┬───────┐
# │ a ┆ b ┆ c │
# │ --- ┆ --- ┆ --- │
# │ f64 ┆ f64 ┆ bool │
# ╞═════╪══════╪═══════╡
# │ 1.0 ┆ 0.5 ┆ true │
# │ 2.0 ┆ 4.0 ┆ true │
# │ 3.0 ┆ 10.0 ┆ false │
# │ 4.0 ┆ 13.0 ┆ true │
# └─────┴──────┴───────┘
Multiple columns can be added by passing a list of expressions.
Multiple columns can be added by passing a list of expressions.
df.with_columns(
[
(Polars.col("a") ** 2).alias("a^2"),
(Polars.col("b") / 2).alias("b/2"),
(Polars.col("c").not_).alias("not c"),
]
)
# =>
# shape: (4, 6)
# ┌─────┬──────┬───────┬─────┬──────┬───────┐
# │ a ┆ b ┆ c ┆ a^2 ┆ b/2 ┆ not c │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ i64 ┆ f64 ┆ bool ┆ i64 ┆ f64 ┆ bool │
# ╞═════╪══════╪═══════╪═════╪══════╪═══════╡
# │ 1 ┆ 0.5 ┆ true ┆ 1 ┆ 0.25 ┆ false │
# │ 2 ┆ 4.0 ┆ true ┆ 4 ┆ 2.0 ┆ false │
# │ 3 ┆ 10.0 ┆ false ┆ 9 ┆ 5.0 ┆ true │
# │ 4 ┆ 13.0 ┆ true ┆ 16 ┆ 6.5 ┆ false │
# └─────┴──────┴───────┴─────┴──────┴───────┘
Multiple columns also can be added using positional arguments instead of a list.
Multiple columns also can be added using positional arguments instead of a list.
df.with_columns(
(Polars.col("a") ** 2).alias("a^2"),
(Polars.col("b") / 2).alias("b/2"),
(Polars.col("c").not_).alias("not c"),
)
# =>
# shape: (4, 6)
# ┌─────┬──────┬───────┬─────┬──────┬───────┐
# │ a ┆ b ┆ c ┆ a^2 ┆ b/2 ┆ not c │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ i64 ┆ f64 ┆ bool ┆ i64 ┆ f64 ┆ bool │
# ╞═════╪══════╪═══════╪═════╪══════╪═══════╡
# │ 1 ┆ 0.5 ┆ true ┆ 1 ┆ 0.25 ┆ false │
# │ 2 ┆ 4.0 ┆ true ┆ 4 ┆ 2.0 ┆ false │
# │ 3 ┆ 10.0 ┆ false ┆ 9 ┆ 5.0 ┆ true │
# │ 4 ┆ 13.0 ┆ true ┆ 16 ┆ 6.5 ┆ false │
# └─────┴──────┴───────┴─────┴──────┴───────┘
Use keyword arguments to easily name your expression inputs.
Use keyword arguments to easily name your expression inputs.
df.with_columns(
ab: Polars.col("a") * Polars.col("b"),
not_c: Polars.col("c").not_
)
# =>
# shape: (4, 5)
# ┌─────┬──────┬───────┬──────┬───────┐
# │ a ┆ b ┆ c ┆ ab ┆ not_c │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ i64 ┆ f64 ┆ bool ┆ f64 ┆ bool │
# ╞═════╪══════╪═══════╪══════╪═══════╡
# │ 1 ┆ 0.5 ┆ true ┆ 0.5 ┆ false │
# │ 2 ┆ 4.0 ┆ true ┆ 8.0 ┆ false │
# │ 3 ┆ 10.0 ┆ false ┆ 30.0 ┆ true │
# │ 4 ┆ 13.0 ┆ true ┆ 52.0 ┆ false │
# └─────┴──────┴───────┴──────┴───────┘
Parameters:
-
exprs
(Array)
—
Column(s) to add, specified as positional arguments. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals.
-
named_exprs
(Hash)
—
Additional columns to add, specified as keyword arguments. The columns will be renamed to the keyword used.
Returns:
4695 4696 4697 |
# File 'lib/polars/data_frame.rb', line 4695 def with_columns(*exprs, **named_exprs) lazy.with_columns(*exprs, **named_exprs).collect(_eager: true) end |