PySpark列表中的项目

时间:2016-12-26 08:17:14

标签: apache-spark pyspark pyspark-sql

以下是我正在努力实现的行动:

types = ["200","300"]
def Count(ID):
    cnd = F.when((**F.col("type") in types**), 1).otherwise(F.lit(0))
    return F.sum(cnd).alias("CountTypes")

粗体语法不正确,有关如何在PySpark中获取正确语法的任何建议吗?

1 个答案:

答案 0 :(得分:3)

我不确定您要实现的目标,但这是正确的语法:

types = ["200","300"]
from pyspark.sql import functions as F

cnd = F.when(F.col("type").isin(types),F.lit(1)).otherwise(F.lit(0))
sum_on_cnd = F.sum(cnd).alias("count_types")
# Column<b'sum(CASE WHEN (type IN (200, 300)) THEN 1 ELSE 0 END) AS `count_types`'>