snowflake.snowpark.DataFrame.with_column¶
- DataFrame.with_column(col_name: str, col: Union[Column, TableFunctionCall]) DataFrame [source]¶
Returns a DataFrame with an additional column with the specified name
col_name
. The column is computed by using the specified expressioncol
.If a column with the same name already exists in the DataFrame, that column is replaced by the new column.
Example 1:
>>> df = session.create_dataframe([[1, 2], [3, 4]], schema=["a", "b"]) >>> df.with_column("mean", (df["a"] + df["b"]) / 2).show() ------------------------ |"A" |"B" |"MEAN" | ------------------------ |1 |2 |1.500000 | |3 |4 |3.500000 | ------------------------
Example 2:
>>> from snowflake.snowpark.functions import udtf >>> @udtf(output_schema=["number"]) ... class sum_udtf: ... def process(self, a: int, b: int) -> Iterable[Tuple[int]]: ... yield (a + b, ) >>> df = session.create_dataframe([[1, 2], [3, 4]], schema=["a", "b"]) >>> df.with_column("total", sum_udtf(df.a, df.b)).sort(df.a).show() ----------------------- |"A" |"B" |"TOTAL" | ----------------------- |1 |2 |3 | |3 |4 |7 | -----------------------
- Parameters:
col_name – The name of the column to add or replace.
col – The
Column
ortable_function.TableFunctionCall
with single column output to add or replace.