小编tyr*_*ode的帖子

pyspark pandas udf RuntimeError:返回的列数与指定的架构不匹配

我在下面定义了 pandas udf

schema2 = StructType([   StructField('sensorid', IntegerType(), True),
    StructField('confidence', DoubleType(), True)]) 

@pandas_udf(schema2,  PandasUDFType.GROUPED_MAP)   
def PreProcess(Indf):   
    confidence=1  
    sensor=Indf.iloc[0,0]   
    df = pd.DataFrame(columns=['sensorid','confidence'])  
    df['sensorid']=[sensor]   
    df['confidence']=[0]   
    return df
Run Code Online (Sandbox Code Playgroud)

然后我将一个包含 3 列的 Spark 数据帧传递到该 udf 中

results.groupby("sensorid").apply(PreProcess)

results:
+--------+---------------+---------------+
|sensorid|sensortimestamp|calculatedvalue|
+--------+---------------+---------------+
|  397332|     1596518086|          -39.0|
|  397332|     1596525586|          -31.0|
Run Code Online (Sandbox Code Playgroud)

但我不断收到此错误:

RuntimeError: Number of columns of the returned pandas.DataFrame doesn't match specified schema.Expected: 3 Actual: 4
Run Code Online (Sandbox Code Playgroud)

我可以说出该错误想要表达的内容,但我不明白该错误是如何弹出的。我认为我正在返回结构中指定的数据帧的正确两列

python pandas apache-spark

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

标签 统计

apache-spark ×1

pandas ×1

python ×1