【问题标题】:When I use UDF to operate a columns, It have a problem当我使用UDF操作列时,它有问题
【发布时间】:2019-05-19 20:20:27
【问题描述】:

当我使用UDF处理一个Column时,我不确定UDF是不是一个一个处理这个列中的元素?如果是这样,我无法理解为什么会出现问题。

import pyspark.sql.types as typ
from pyspark.sql.functions import udf,pandas_udf, PandasUDFType
def parse_model(v):
    return v.split(' ')
Parse_model=pandas_udf(parse_model,typ.ArrayType(typ.StringType(),True))
sample_data_df.withColumn('Models',Parse_model('Model')).show(

应该是 细绳 在列中而不是系列中。

AttributeError: 'Series' object has no attribute 'split'

【问题讨论】:

    标签: pandas pyspark user-defined-functions


    【解决方案1】:

    Scalar Pandas 用户定义函数采用pandas.Series 并将结果作为pandas.Series 返回。

    由于 v 是 Series 类型,因此您会收到错误消息。像下面这样更新 udf 将解决此问题。

    def parse_model(v): 
       return pd.Series([v[0].split(' ')])
    

    【讨论】:

    • 谢谢!它解决了问题。但是你不觉得pandas_udf更复杂,因为它涉及到对象和Pd.series之间的两步转换吗?
    猜你喜欢
    • 2018-09-08
    • 2023-01-17
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多