本文介绍了从 pyspark agg 函数到 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按 pyspark 上的条件计算行数

I am counting rows by a condition on pyspark

df.agg(count(when((col("my_value")==0),True))).show()

它按我的预期工作.那么如何提取表中显示的值存储到Python变量中?

It works as I expected. Then how can I extract the value showed in the table to store to a Python variable?

推荐答案

如果你只想计算 Trues (ceros),你最好这样做:

If you just want to count the Trues (ceros), you should better do this:

from pyspark.sql import functions as F
pythonVariable = df.where(F.col('my_value') == 0).collect[0][0]

如您所见,无需将 ceros 更改为 True 即可对其进行计数.

As you can see, there is no need to change the ceros to True to count them.

这篇关于从 pyspark agg 函数到 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:03