python - PySpark 代码引发:TypeError:float() 参数必须是字符串或数字
问题描述
我有以下 PySpark 代码,除了今天,它一直运行良好:
row_stats = dataframe
.withColumn("exploded" , explode(col("products")))
.withColumn("score", col("exploded").getItem(target_field))
.where(col("score").isNotNull())
.select(mean_(col("score")).alias("mean"),stddev_(col("score")).alias("stddev")).first()
mean = 0
std = 0
if row_stats is not None:
print "row_stats.mean"
print row_stats.mean
mean = Decimal(float(row_stats.mean))
std = Decimal(float(row_stats.stddev))
我在该行收到错误mean = Decimal(float(row_stats.mean))
:
TypeError: float() argument must be a string or a number
print
输出:
<type 'NoneType'>
None
如何正确处理此错误以获取mean
和std
等于 0?
解决方案
这应该通过将平均对象转换为十进制来与Pyspark一起使用:
row_stats.mean.cast('decimal')
推荐阅读
- python - 将 PDF 添加到 sqlalchemy 数据库
- sql-server - GIS 中使用的 SQL 关键字“CONVERT”附近的语法不正确 SQL GIS
- android - RN查看android原生活动
- angular - Ionic 4 - 如何将组件动态添加到页面?
- networking - 无法查看从 terraform 资源创建的 EKS 集群
- r - 在另一个类的方法中使用一个类的方法
- python - 寻找系列中的最低值
- asp.net-core - 弹性搜索 Nest Client 很慢
- react-native - 将自定义列表传递给 React 本机导航 5.5 DrawerItemList
- python - 如何使用用户名作为 slug 参数 django rest 框架获取 url