我正在使用PySpark并遇到一个问题,其中isin方法通过忽略其输入col的结果来返回所有结果,在我的情况下是忽略_getValidCols fn的返回。参见下面的代码:
min_supplier = 12
def _getValidCols(df,agg_lvl):
return df.groupby(agg_lvl) \
.agg(F.countDistinct("supplier") \
.alias('distinct_suppliers')) \
.filter("distinct_suppliers >= {}".format(min_supplier))
df = df.withColumn('test_col_123',\
F.when(df.SUB_SC_DESC.isin(_getValidCols(df,"col_name_1").col_name_1),df.col_name_1) \
.otherwise("Unassigned")
)
调用函数_getValidCols时,将返回带有结果的有效df。见下文:
_getValidCols(df,"col_name_1").show()
+--------------------+------------------+
| col_name_1 |distinct_suppliers|
+--------------------+------------------+
|test_value_123... | 485|
+--------------------+------------------+
将上面返回的指定col(col_name_1)传递给isin方法时,将返回无意义。我们希望在结果col test_col_123中仅看到“ test_value_123 ...”或“未指定”的结果,但是将返回所有出现的df.col_name_1(意味着df.col_name_1 = df.test_col_123)。
有人可以看到我在上面犯的错误吗?正在向isin方法传递类型为pyspark.sql.column的有效col
此外,如果我正在执行任何一种这样的低效方式,请随时指出。
谢谢。