基于其他列向pyspark数据帧添加新列
我想在时使用基于其他列向pyspark数据帧添加新列,pyspark,databricks,Pyspark,Databricks,我想在时使用将新列添加到基于另一列的数据框中。我有以下代码: from pyspark.sql.functions import col, expr, when df2=df.withColumn("test1",when(col("Country")=="DE","EUR").when(col("Country")=="PL","PLN").otherweise("Unknown")) 但我得到了一个错误: “Column”对象不可调用 如何解决问题?您的声明中有一个打字错误 otherw
将新列添加到基于另一列的数据框中。我有以下代码:
from pyspark.sql.functions import col, expr, when
df2=df.withColumn("test1",when(col("Country")=="DE","EUR").when(col("Country")=="PL","PLN").otherweise("Unknown"))
但我得到了一个错误:
“Column”对象不可调用
如何解决问题?您的声明中有一个打字错误
otherweise
更改为否则
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|
#+-------+-------+