Skip to main content

withColumns

Returns a new DataFrame by adding multiple columns or replacing the existing columns that have the same names.

Syntax

withColumns(*colsMap: Dict[str, Column])

Parameters

Parameter

Type

Description

colsMap

dict

a dict of column name and Column. Currently, only a single map is supported.

Returns

DataFrame: DataFrame with new or replaced columns.

Examples

Python
df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"])
df.withColumns({'age2': df.age + 2, 'age3': df.age + 3}).show()
# +---+-----+----+----+
# |age| name|age2|age3|
# +---+-----+----+----+
# | 2|Alice| 4| 5|
# | 5| Bob| 7| 8|
# +---+-----+----+----+