withColumn在pyspark中没有给出groupby的预期结果



我有一个数据框架,看起来像下面

+----------+---------------+---------+-----------------+----------------------------+
|CustomerNo|TransactionDate|SKUItemID|one_day_back_date|last_12month_date_from_trans|
+----------+---------------+---------+-----------------+----------------------------+
|   10080.0|     2020-08-04|  1297636|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297637|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297638|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297639|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297640|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297642|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297643|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297644|       2020-08-03|                  2019-08-04|
|   10080.0|     2020-08-04|  1297645|       2020-08-03|                  2019-08-04|
|   10080.0|     2018-06-26|    33559|       2020-08-03|                  2017-06-26|
|   10080.0|     2018-07-03|    36725|       2020-08-03|                  2017-07-03|
|   10080.0|     2018-07-03|    36726|       2020-08-03|                  2017-07-03|
|   10080.0|     2018-07-03|    36727|       2020-08-03|                  2017-07-03|
|   10080.0|     2018-07-03|    36728|       2020-08-03|                  2017-07-03|
|  216988.0|     2019-12-24|   812294|       2019-12-23|                  2018-12-24|
|  216988.0|     2019-12-24|   812298|       2019-12-23|                  2018-12-24|
+----------+---------------+---------+-----------------+----------------------------+

我需要根据我在下面的代码

中使用的日期条件获得总项目
c=x.withColumn('total_items',(F.col('TransactionDate')<F.col('one_day_back_date')) & (F.col('TransactionDate') >= F.col('last_12month_date_from_trans'))) 
.groupBy('CustomerNo').agg(F.count('SKUItemID').alias('total_items'))

我需要获得'5'作为客户10080.0的total_items,但是当我应用上述代码时,我获得'14'作为total_items。我也使用了过滤器,但是当应用于大型数据集

时,它不会给出所有的行。得到的输出

+----------+-----------+
|CustomerNo|total_items|
+----------+-----------+
|   10080.0|         14|
+----------+-----------+

所需输出:

+----------+-----------+
|CustomerNo|total_items|
+----------+-----------+
|   10080.0|         5 
216988.0          0
+----------+-----------+

谁能告诉我我哪里做错了?

您正在创建一个新列total_items,但是您没有使用它做任何事情。我觉得你应该用滤镜。仕

c=x.where((F.col('TransactionDate')<F.col('one_day_back_date')) & (F.col('TransactionDate') >= F.col('last_12month_date_from_trans'))) 
.groupBy('CustomerNo').agg(F.count('SKUItemID').alias('total_items'))

这将获取满足条件的行,然后计算每组中的记录数。

也可以得到零计数,您可以这样做:

c=(x.withColumn('toCount', 
F.when( (F.col('TransactionDate')<F.col('one_day_back_date')) 
& 
(F.col('TransactionDate') >= F.col('last_12month_date_from_trans'))
, 1)
.otherwise(0) 
)
.groupBy('CustomerNo')
.agg(F.sum('toCount').alias('total_items')))

toCount列用1标记计数项,并在toCount列p.组中求和。

最新更新