为什么这种PySpark-isin映射不起作用

2024-10-03 19:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码

listOTHERS_hpos = ['BLACKBERRY 7', 'SYMBIAN', 'NOKIA OS', 'BLACKBERRY 10', 'WINDOWS']
lastvalue_month = lastvalue_month.withColumn('handset_type', 
                        when(col('handset_os').isin(listOTHERS_hpos) , lit('OTHERS')))
lastvalue_month.groupBy('handset_os').count().orderBy('count').show()

这是输出

+-------------+-----+
|   handset_os|count|
+-------------+-----+
|      WINDOWS|    6|
|BLACKBERRY 10|    8|
|     NOKIA OS|   15|
|      SYMBIAN|   39|
| BLACKBERRY 7|   43|
|    UNDEFINED| 1218|
|    APPLE IOS| 1496|
|        OTHER| 3705|
|      ANDROID|13218|
+-------------+-----+

按照代码应该是这样的

+-------------+-----+
|   handset_os|count|
+-------------+-----+
|       OTHERS|  111|
|    UNDEFINED| 1218|
|    APPLE IOS| 1496|
|        OTHER| 3705|
|      ANDROID|13218|
+-------------+-----+

Tags: appleoswindowscountundefinedmonthothersnokia
1条回答
网友
1楼 · 发布于 2024-10-03 19:31:27

应使用正确的列对列进行分组。此外,在when中使用.otherwise()是有帮助的:

lastvalue_month = lastvalue_month.withColumn('handset_type', 
                        when(col('handset_os').isin(listOTHERS_hpos) , lit('OTHERS')).otherwise(col('handset_type')))
lastvalue_month.groupBy('handset_type').count().orderBy('count').show()

相关问题 更多 >