Databricks NameError: name ‘expr’ is defined.

huangapple go评论54阅读模式
英文:

Databricks NameError: name 'expr' is not defined

问题

在尝试在Databricks中执行以下Spark代码时,我收到以下错误:

NameError: 名称 'expr' 未定义

%python
df = sql("select * from xxxxxxx.xxxxxxx")
transfromWithCol = (df.withColumn("MyTestName", expr("case when first_name = 'Peter' then 1 else 0 end")))

有何想法?

英文:

When attempting to execute the following spark code in Databricks I get the error:

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")))

Any thoughts

答案1

得分: 1

  • 我尝试了类似的代码时,也收到了相同的错误。以下是我收到的错误。
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()

Databricks NameError: name ‘expr’ is defined.

  • 为了使此代码正常工作,您必须安装来自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()

Databricks NameError: name ‘expr’ is defined.

英文:
  • I have received the same error when I have tried a similar code. The following is the error that I have received.
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()

Databricks NameError: name ‘expr’ is defined.

  • You have to install the function from pyspark.sql.functions in order to for this to work. Using the following modified code has worked without any issue:
from pyspark.sql.functions import expr

df1 = df.withColumn("MyTestName", expr("case  when gname =  'Ana'  then  1  else  0  end"))
df1.show()

Databricks NameError: name ‘expr’ is defined.

答案2

得分: 1

你好,以下是已翻译的内容:

"Looks like you forgot to import "expr" first from the library. Following is the import statement to avert the encountered error:"
"似乎你忘记了首先从库中导入"expr"。以下是导入语句以避免遇到的错误:"

from pyspark.sql.functions import expr
英文:

Looks like you forgot to import "expr" first from the library. Following is the import statement to avert the encountered error:

from pyspark.sql.functions import expr

huangapple
  • 本文由 发表于 2023年6月8日 18:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定