2

Suppose I have a Spark Scala DataFrame object like:

+----------------+ |col1 |col2 | +----------------+ |1 |2 | |3 |4 | +----------------+

And I want a DataFrame like: +--------------------+ |col1 |col2 |col3 | +--------------------+ |1 |2 |3 | |3 |4 |7 | +--------------------+

Which adds col1 and col2 to col3, could anyone please tell me how to do that? WithColumn takes only one column as parameter whereas I need to take two columns.

2

2 Answers 2

3

You can use withColumn or select as

val df = Seq(
  (1,2),
  (3,4)
).toDF("col1", "col2")

df.withColumn("col3", $"col1" + $"col2").show(false)

df.select($"col1", $"col2", ($"col1" + $"col2").as("col3")).show(false)

Output:

+----+----+----+
|col1|col2|col3|
+----+----+----+
|1   |2   |3   |
|3   |4   |7   |
+----+----+----+
Sign up to request clarification or add additional context in comments.

Comments

0

WithColumn takes two parameters a name and a function that should result in a type column - so a function or expression whose result is a column is valid hence you can do the below (or similar)

df.withColumn("col3", df("col1")+df("col2")) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.