英文:
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()
- 为了使此代码正常工作,您必须安装来自
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()
英文:
- 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()
- 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()
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论