我想使用将新列添加到基于另一列的数据框WHEN
。我有以下代码:
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' object is not callable
如何解决该问题?
您的陈述中有错字。
otherweise
改成 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|
#+-------+-------+
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句