首页 > 解决方案 > 超出表中每一列的范围计数

问题描述

我正在尝试获取表中所有列的超出范围的值。首先,我根据下面的代码得到超出范围的最大值和最小值,然后尝试遍历表中的所有记录以获取超出范围的每一列的记录数。当表大小很小时,下面的函数效果很好,但是当表大小以十亿为单位时,它需要很长时间才能检查每条记录。当表格很大时,他们是否有更好的方法来获得超出范围的计数。

out_of_range_max_value = spark_df.select([(F.round(F.mean(F.col(c).cast('int')),0) + 3* F.round(F.stddev(F.col(c).cast('int')),0)).alias(c) for c in spark_df.columns if dict(spark_df.dtypes)[c] not in ('timestamp', 'string', 'date','array<string>')])

out_of_range_min_value = spark_df.select([(F.round(F.mean(F.col(c).cast('int')),0) - 3* F.round(F.stddev(F.col(c).cast('int')),0)).alias(c) for c in spark_df.columns if dict(spark_df.dtypes)[c] not in ('timestamp', 'string', 'date','array<string>')])

df = spark_df.agg(*(F.count(F.when((F.col(c).cast('int') > out_of_range_pdfNA[c].astype(int).values.tolist()[0]) | (F.col(c).cast('int') < out_of_range_pdfNA[c].astype(int).values.tolist()[1]), c)).alias(c)  for c in spark_df.columns if dict(spark_df.dtypes)[c] not in ('timestamp', 'string', 'date','array<string>') ))



标签: pythonpyspark

解决方案


推荐阅读