在pyspark的情况总和(sum of case when in pyspark)

编程入门 行业动态 更新时间:2024-10-24 16:24:03
在pyspark的情况总和(sum of case when in pyspark)

我正在尝试将hql脚本转换为pyspark。 我正在努力如何在groupby子句之后的聚合语句中实现大小写的总和。 例如。

dataframe1 = dataframe0.groupby(col0).agg( SUM(f.when((col1 == 'ABC' | col2 == 'XYZ'), 1).otherwise(0)))

在pyspark有可能吗? 我在执行这样的语句时遇到错误。 谢谢

I am trying convert hql script into pyspark. I am struggling how to achieve sum of case when statements in aggregation after groupby clause. eg.

dataframe1 = dataframe0.groupby(col0).agg( SUM(f.when((col1 == 'ABC' | col2 == 'XYZ'), 1).otherwise(0)))

Is it possible in pyspark? I am getting error while executing such statement. Thanks

最满意答案

您可以使用withColumn创建一个包含要求求和的值的列,然后对其进行聚合。 例如:

from pyspark.sql import functions as F, types as T schema = T.StructType([ T.StructField('key', T.IntegerType(), True), T.StructField('col1', T.StringType(), True), T.StructField('col2', T.StringType(), True) ]) data = [ (1, 'ABC', 'DEF'), (1, 'DEF', 'XYZ'), (1, 'DEF', 'GHI') ] rdd = sc.parallelize(data) df = sqlContext.createDataFrame(rdd, schema) result = df.withColumn('value', F.when((df.col1 == 'ABC') | (df.col2 == 'XYZ'), 1).otherwise(0)) \ .groupBy('key') \ .agg(F.sum('value').alias('sum')) result.show(100, False)

打印出这个结果:

+---+---+ |key|sum| +---+---+ |1 |2 | +---+---+

You can use withColumn to create a column with the values you want to to be summed, then aggregate on that. For example:

from pyspark.sql import functions as F, types as T schema = T.StructType([ T.StructField('key', T.IntegerType(), True), T.StructField('col1', T.StringType(), True), T.StructField('col2', T.StringType(), True) ]) data = [ (1, 'ABC', 'DEF'), (1, 'DEF', 'XYZ'), (1, 'DEF', 'GHI') ] rdd = sc.parallelize(data) df = sqlContext.createDataFrame(rdd, schema) result = df.withColumn('value', F.when((df.col1 == 'ABC') | (df.col2 == 'XYZ'), 1).otherwise(0)) \ .groupBy('key') \ .agg(F.sum('value').alias('sum')) result.show(100, False)

Which prints out this result:

+---+---+ |key|sum| +---+---+ |1 |2 | +---+---+

更多推荐

本文发布于:2023-08-05 09:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   情况   pyspark   sum   case

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!