【发布时间】:2020-12-03 18:21:53
【问题描述】:
我在下面定义了 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
然后我将一个包含 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 列
【问题讨论】:
-
另外,我在集群上的数据块笔记本中运行它。集群信息:42GB | 12核 | DBR6.4 |火花 2.4.5
-
@tryingtocode 你找到解决办法了吗
标签: python pandas apache-spark