小编Dav*_*aub的帖子

在 pyspark DataFrame 中创建某个类型的空数组列

我尝试向 df 添加一个包含空字符串数组的列,但最终添加了一列字符串数组。

我试过这个:

import pyspark.sql.functions as F
df = df.withColumn('newCol', F.array([]))
Run Code Online (Sandbox Code Playgroud)

我怎样才能在 pyspark 中做到这一点?

python dataframe apache-spark pyspark

8
推荐指数
2
解决办法
1万
查看次数

如何在没有 UDF 的情况下计算 PySpark 数据帧中数组列中的尾随零

我有一个数据框,其中有一列具有固定数量整数的数组。如何将包含数组中尾随零数的列添加到 df 中?我想避免使用 UDF 以获得更好的性能。

例如,输入 df:

>>> df.show()
+------------+
|           A|
+------------+
| [1,0,1,0,0]|
| [2,3,4,5,6]|
| [0,0,0,0,0]|
| [1,2,3,4,0]|
+------------+
Run Code Online (Sandbox Code Playgroud)

和一个想要的输出:

>>> trailing_zeroes(df).show()
+------------+-----------------+
|           A|   trailingZeroes|
+------------+-----------------+
| [1,0,1,0,0]|                2|
| [2,3,4,5,6]|                0|
| [0,0,0,0,0]|                5|
| [1,2,3,4,0]|                1|
+------------+-----------------+
Run Code Online (Sandbox Code Playgroud)

python apache-spark apache-spark-sql pyspark

6
推荐指数
1
解决办法
778
查看次数

如何在 python 中创建一个代理对象作为底层对象

我有一个带有底层对象的代理类。我希望将代理对象传递给需要底层对象类型的函数。如何使代理类型与底层对象匹配?

class Proxy:
    def __init__(self, obj):
        self.obj = obj

    def __getattribute__(self, name):
        return getattr(self.obj, name)

    def __setattr__(self, name, value):
        setattr(self.obj, name, value)

def foo(bar: MyClass):
    ...

foo(Proxy(MyClass())) # Warning: expected 'MyClass', got 'Proxy' instead
Run Code Online (Sandbox Code Playgroud)

python design-patterns python-typing

6
推荐指数
1
解决办法
1159
查看次数

在 PySpark 中将整数列转换为字符串 IP

我有一个 pyspark 数据帧,其中 IPv4 值为整数,我想将它们转换为字符串形式。最好没有可能对性能产生很大影响的 UDF。

示例输入:

+---------------+
|         IP_int|
+---------------+
|       67633643|
|      839977746|
|      812147536|
+---------------+
Run Code Online (Sandbox Code Playgroud)

示例输出:

+---------------+
|         IP_str|
+---------------+
|      4.8.1.235|
|    50.17.11.18|
|   48.104.99.80|
+---------------+
Run Code Online (Sandbox Code Playgroud)

python apache-spark pyspark

1
推荐指数
1
解决办法
223
查看次数