首页 > 解决方案 > 比较具有特定误差范围的值

问题描述

有没有办法double在指定的误差范围内比较 PySpark 中的两个类型值?Essential 类似于这篇文章,但在 PySpark 中。

就像是:

df=#some dataframe with 2 columns RESULT1 and RESULT2

df=withColumn('compare', when(col('RESULT1')==col('RESULT2') +/- 0.05*col('RESULT2'), lit("match")).otherwise(lit("no match"))

但是以更优雅的方式?

标签: apache-sparkpysparkapache-spark-sqlcompare

解决方案


您可以between用作条件:

df2 = df.withColumn(
    'compare',
    when(
        col('RESULT1').between(0.95*col('RESULT2'), 1.05*col('RESULT2')), 
        lit("match")
    ).otherwise(
        lit("no match")
    )
)

推荐阅读