我有以下DF示例:
------------------+---------+
| ticket_id|news_item|
+--------------------+---------+
|13001244822020121...| 1|
|13000386812020121...| 0|
|13000521120201212...| 0|
|13000578320201212...| 0|
|13000667320201212...| 0|
|13001430320201212...| 0|
|13001440320201212...| 0|
|13001497120201212...| 0|
|13001518420201212...| 0|
|13001117120201212...| 1|
我想做的是基于news_item列计算所有ticket_id。因此,例如,如果DF有300行,则所需的输出应为:
new_column_name |news_item|
+--------------------+---------+
|200 | 1|
|100 | 0|
我正在使用的代码如下:
output = (df
.groupby('is_lidl_plus','news_item')
.agg(f.countDistinct('ticket_id').alias('total_tickets'),
**f.countDistinct(f.when('news_item')==1)).alias('tickets_with_vouchure')**,
f.round(f.sum('gross_spending'),2).alias('total_amount_spend'),
f.round(f.avg('gross_spending'),2).alias('average_gross_amount_spend'),
f.round(f.avg('net_spending'),2).alias('average_amount_spend')))
pyspark的输出是:
TypeError:when()缺少1个必需的位置参数:“ value”
有什么线索吗?
我认为您是要这样做?您想计算带有news_item = 1的ticket_id的数量吗?
f.countDistinct(
f.when(
f.col('news_item')==1,
f.col('ticket_id')
)
).alias('tickets_with_vouchure')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句