如何解决pyspark pandas udf RuntimeError:返回的列数与指定的架构不匹配
我在下面定义了熊猫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
然后我将一个3列的spark数据帧传递到该udf
results.groupby("sensorid").apply(PreProcess)
results:
+--------+---------------+---------------+
|sensorid|sensortimestamp|calculatedvalue|
+--------+---------------+---------------+
| 397332| 1596518086| -39.0|
| 397332| 1596525586| -31.0|
但是我一直收到此错误:
RuntimeError: Number of columns of the returned pandas.DataFrame doesn't match specified schema.Expected: 3 Actual: 4
我可以说出该错误试图说什么,但我不知道该错误如何弹出。我以为我要返回结构中指定的数据框的正确2列
解决方法
apply
已被弃用,似乎期望返回相同的输入列,在本例中为 3。尝试将 applyInPandas
与预期的输出架构一起使用:
results.groupby("sensorid").applyInPandas(PreProcess,schema=schema2)
更新了最新版本的链接。 (Spark 的文档更改和链接已损坏)
在 3.0.0 版中:apply
applyInPandas
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。