python中case语句的问题

6rqinv9w  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(446)

我使用下面的代码得到一个Dataframe,但得到错误。如果将从代码中删除case语句,那么它就可以正常工作。

from pyspark.sql import functions as F
ivr_icm_cfs_lookup = spark.table("sample_table").where(F.col("tier") == "Servicing").select("fig","fig_bu","tier") .withColumn("fig",F.regexp_replace(F.col("fig"),"[^0-9a-zA-Z]",' ')).withColumn("fig_bu",F.when((F.col("fig_bu") == 'OPEN' & F.col("tier") == 'Academy', F.upper("tier")).otherwise(F.col("fig_bu"))))
mctunoxg

mctunoxg1#

先生,你的支架有问题。现在试试

from pyspark.sql import functions as F
ivr_icm_cfs_lookup = spark.table("sample_table").where(F.col("tier") == "Servicing").select("fig","fig_bu","tier") .withColumn("fig",F.regexp_replace(F.col("fig"),"[^0-9a-zA-Z]",' ')).withColumn("fig_bu",F.when((F.col("fig_bu") == 'OPEN') & (F.col("tier") == 'Academy'), F.upper("tier")).otherwise(F.col("fig_bu")))

如果这确实不适合您,那么您可以尝试sparksql,方法是将dataframe注册为table,然后在sql中编写案例。

from pyspark.sql import functions as F
ivr_icm_cfs_lookup = spark.table("sample_table").where(F.col("tier") == "Servicing").select("fig","fig_bu","tier") .withColumn("fig",F.regexp_replace(F.col("fig"),"[^0-9a-zA-Z]",' '))

ivr_icm_cfs_lookup.createOrReplaceTempView('ivr_icm_cfs_lookup_table')
ivr_icm_cfs_lookup_df = spark.sql('''
select *, case when fig_bu='OPEN' and tier='Academy' then upper(tier) else fig_bu END as fig_bu_2 from ivr_icm_cfs_lookup_table
'''

这样可以添加所需的列。

相关问题