python - 跨数据框的pyspark窗口函数(使用lambda?)
问题描述
我有形状的数据框(5,100)
Idx col_1 col_2 col_3 col_4 col_5
0 0.118126 0.248718 0.717174 0.551654 0.832513
1 0.823785 0.432577 0.280622 0.779573 0.917391
2 0.897924 0.388673 0.746616 0.560313 0.793524
3 0.771598 0.620364 0.406896 0.194094 0.652458
4 0.837725 0.335048 0.867820 0.493559 0.697139
5 0.721675 0.893441 0.855906 0.458607 0.806025
6 0.338725 0.732343 0.551936 0.840650 0.405240
7 0.314990 0.062280 0.145607 0.202780 0.999613
8 0.195877 0.176967 0.934697 0.028677 0.300465
9 0.643504 0.703770 0.173629 0.880687 0.290086
10 0.356232 0.400717 0.782214 0.984976 0.098428
我已将窗口定义如下:
window = Window.orderBy("Idx").rowsBetween( Window.unboundedPreceding,10)
并注册了一个自定义函数,如下所示:
@pandas_udf(FloatType(), PandasUDFType.GROUPED_AGG)
def custom_func(x: pd.Series) -> float:
do something
return y
我知道如何在一列上使用这个 custom_func:
df.withColumn("results", custom_func(func.col("col_1")).over(window)).orderBy("Idx")
但是,我想在整个数据框(所有 5 列)上使用这个 custom_func,并希望输出为 5 列。任何指向正确方向的指针都会非常有帮助。
解决方案
推荐阅读
- javascript - Javascript - 依赖注入?
- mysql - MYSQL / MariaDB 触发器 - 更新后
- laravel - 注册和登录 API 在 Laravel7 中不起作用,但没有 API,它可以通过 web.php 正常工作
- windows-forms-designer - 如何在 Windows 窗体中使用来自动态生成的文本框的输入?
- mysql - 索引 MySQL 表以搜索任何列组合
- c++ - 带有继承的 C++ 类设计
- python - 数据框中行的所有组合
- flutter - 为 itemBuilder 使用提取的小部件时,ReorderableListView 会引发错误?
- c++ - 为什么带有 2 个空格的 char 打印一个 int 值?
- flutter - 如何在 onSelected/onTap 中使用 switch case