Implementation:Eventual Inc Daft DataFrame With Column
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, Data_Transformation |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for adding or replacing a column in a DataFrame using a computed expression provided by the Daft library.
Description
The with_column method on Daft's DataFrame class adds a new column with the given name and expression to the DataFrame. If a column with the same name already exists, it is replaced. Internally, this is equivalent to calling with_columns({column_name: expr}), which itself performs a SELECT of all existing columns plus the new expression aliased to the given name.
Usage
Use df.with_column() when you need to add a single computed column to an existing DataFrame. For adding multiple columns at once, consider df.with_columns() instead.
Code Reference
Source Location
- Repository: Daft
- File:
daft/dataframe/dataframe.py - Lines: L2408-2441
Signature
def with_column(self, column_name: str, expr: Expression) -> DataFrame
Import
import daft
# Method on DataFrame - no separate import needed
df.with_column("new_col", daft.col("x") + 1)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| column_name | str | Yes | Name of the new column (or existing column to replace) |
| expr | Expression | Yes | Expression to compute the column values |
Outputs
| Name | Type | Description |
|---|---|---|
| return | DataFrame | A new DataFrame with all existing columns plus the new (or replaced) column |
Usage Examples
Basic Usage
import daft
df = daft.from_pydict({"x": [1, 2, 3]})
# Add a computed column
new_df = df.with_column("x+1", df["x"] + 1)
new_df.show()
# Output:
# x: [1, 2, 3]
# x+1: [2, 3, 4]
Replace Existing Column
import daft
df = daft.from_pydict({"x": [1, 2, 3], "y": [10, 20, 30]})
# Replace column "y" with a new expression
new_df = df.with_column("y", df["y"] * 2)
new_df.show()
# Output:
# x: [1, 2, 3]
# y: [20, 40, 60]