1

I would like to add a new column to a dataframe based on another column using WHEN. I have the folowing code:

from pyspark.sql.functions import col, expr, when
df2=df.withColumn("test1",when(col("Country")=="DE","EUR").when(col("Country")=="PL","PLN").otherweise("Unknown"))

but I get the error: 'Column' object is not callable How can I fix the problem?

1 Answer 1

1

You have a typo in your statement.

  • otherweise change to otherwise

df=spark.createDataFrame([("DE",),("PL",),("PO",)],["Country"])
df.withColumn("test1",when(col("country") == "DE", "EUR").when(col("country") == "PL", "PLN").otherwise("Unknown")).show()
#+-------+-------+
#|Country|  test1|
#+-------+-------+
#|     DE|    EUR|
#|     PL|    PLN|
#|     PO|Unknown|
#+-------+-------+
Sign up to request clarification or add additional context in comments.

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.