这是我的密码
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|
+-------------+-----+发布于 2021-09-13 08:24:21
应使用正确的列对列进行分组。此外,在.otherwise()中使用when是有帮助的。
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()https://stackoverflow.com/questions/69157909
复制相似问题