PySpark 当列表中的项目 [英] PySpark When item in list

查看:12
本文介绍了PySpark 当列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我要实现的操作:

Following is the action I'm trying to achieve:

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 获取正确的语法吗?

The syntax in bold is not correct, any suggestions how to get the right syntax here for PySpark?

推荐答案

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

I'm not sure about what you are trying to achieve but here is the correct syntax :

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`'>

这篇关于PySpark 当列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆