将窗口功能应用于多列

编程入门 行业动态 更新时间:2024-10-24 06:24:28
本文介绍了将窗口功能应用于多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想执行窗口功能(平均移动平均值),但是要在数据框的所有列上执行.

I would like to perform window function (concretely moving average), but over all columns of a dataframe.

我可以这样

from pyspark.sql import SparkSession, functions as func df = ... df.select([func.avg(df[col]).over(windowSpec).alias(col) for col in df.columns])

但是我担心这不是很有效.有更好的方法吗?

but I'm afraid this isn't very efficient. Is there a better way to do it?

推荐答案

一个更好的选择是创建一个新的df,在其中您可以对Window函数中的列进行分组,并对其余的列应用平均值,然后进行左连接.对于将df溢出到磁盘(或无法持久存储在内存中)的大型数据帧,这绝对是最佳选择.

An alternative which may be better is to create a new df where you Group By the columns in Window function and apply average on the remaining columns then do a left join. For large data frames where the df is being spilled over to disk (or cannot be persisted in memory), this will definitely be more optimal.

更多推荐

将窗口功能应用于多列

本文发布于:2023-10-24 16:56:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   窗口   功能

发布评论

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

>www.elefans.com

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