Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
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|
# +---+-----+----+----+