【发布时间】:2020-07-27 16:53:46
【问题描述】:
对于表 1 中的每一行,我正在尝试计算表 2 中的行数,并根据表 1 中的值满足条件。
表 1 中的年龄应介于表 2 的 StartAge 和 EndAge 之间,或者等于 StartAge 和 EndAge。
这可以使用 udf 和 withColumn 吗?我尝试了几种方法来做到这一点,例如使用 withColumn 和 withColumn 与 UDF,但两种方法都失败了。
def counter(a):
return table2.where((table2.StartAge <= a) & (table2.EndAge >=a)).count()
counter_udf = udf(lambda age: counter(age), IntegerType())
table1 = table1.withColumn('Count', counter_udf('Age ID'))
这有意义吗? 谢谢。
输入输出示例:
【问题讨论】:
-
Please don't post images of code/data (or links to them) ,最好将它们粘贴为文本,以便用户复制数据
标签: python apache-spark pyspark apache-spark-sql databricks