当尝试在Databricks中执行以下spark代码时,我得到错误:
NameError: name 'expr' is not defined %python df = sql("select * from xxxxxxx.xxxxxxx") transfromWithCol = (df.withColumn("MyTestName", expr("case when first_name = 'Peter' then 1 else 0 end")))
有什么想法吗
pdsfdshx1#
data = [[1,'Ana'],[2,'Topson'],[3,'Ceb']] df = spark.createDataFrame(data=data,schema=['id','gname']) df.show() df1 = df.withColumn("MyTestName", expr("case when gname = 'Ana' then 1 else 0 end")) df1.show()
pyspark.sql.functions
from pyspark.sql.functions import expr df1 = df.withColumn("MyTestName", expr("case when gname = 'Ana' then 1 else 0 end")) df1.show()
1条答案
按热度按时间pdsfdshx1#
pyspark.sql.functions
安装函数才能使其工作。使用以下修改后的代码可以正常工作,没有任何问题: