当前位置:网站首页>pyspark df secondary sorting
pyspark df secondary sorting
2022-08-03 07:41:00 【WGS.】
需求
According to the two columns of sorts,根据列1降序,当列1At the same time, according to the column2降序
假数据
di = [{
'suuid': 'DONEW', 'oaid': '000-12','y': 1},
{
'suuid': 'DONEW', 'oaid': '000-12','y': 0},
{
'suuid': 'WONF', 'oaid': '111-12','y': 0},
{
'suuid': 'O000-2FJA01', 'oaid': '111-12','y': 1},
{
'suuid': 'F1NS', 'oaid': '111-12','y': 0},
{
'suuid': 'F1NS', 'oaid': '111-12','y': 0},
{
'suuid': 'F1NS', 'oaid': '111-12','y': 1},
{
'suuid': 'WONF', 'oaid': '000-12','y': 0}]
df = ss.createDataFrame(di)
df.show()
+------+-----------+---+
| oaid| suuid| y|
+------+-----------+---+
|000-12| DONEW| 1|
|000-12| DONEW| 0|
|111-12| WONF| 0|
|111-12|O000-2FJA01| 1|
|111-12| F1NS| 0|
|111-12| F1NS| 0|
|111-12| F1NS| 1|
|000-12| WONF| 0|
+------+-----------+---+
实现
- ascending:The incoming list,
0代表降序、1代表升序

def row_count(row):
suuid, y = row[0], row[1]
clicks = sum(y)
pvs = len(y) - clicks
return suuid, pvs, clicks
dfsuuid = df.groupBy('suuid').agg(fn.collect_list('y').alias('y'))\
.rdd.map(row_count).toDF(schema=['suuid', 'pvs', 'clicks'])
dfsuuid.show()
# pvs降序、clicks降序
dfsuuid.orderBy(['pvs', 'clicks'], ascending=[0, 0]).show()
+-----------+---+------+
| suuid|pvs|clicks|
+-----------+---+------+
| F1NS| 2| 1|
|O000-2FJA01| 0| 1|
| WONF| 2| 0|
| DONEW| 1| 1|
+-----------+---+------+
+-----------+---+------+
| suuid|pvs|clicks|
+-----------+---+------+
| F1NS| 2| 1|
| WONF| 2| 0|
| DONEW| 1| 1|
|O000-2FJA01| 0| 1|
+-----------+---+------+
验证0为降序、1为升序
dfsuuid.orderBy(['pvs', 'clicks'], ascending=[1, 0]).show()
dfsuuid.orderBy(['pvs', 'clicks'], ascending=[1, 1]).show()
+-----------+---+------+
| suuid|pvs|clicks|
+-----------+---+------+
|O000-2FJA01| 0| 1|
| DONEW| 1| 1|
| F1NS| 2| 1|
| WONF| 2| 0|
+-----------+---+------+
+-----------+---+------+
| suuid|pvs|clicks|
+-----------+---+------+
|O000-2FJA01| 0| 1|
| DONEW| 1| 1|
| WONF| 2| 0|
| F1NS| 2| 1|
+-----------+---+------+
边栏推荐
- 【playwright】pytest-playwright增加代理服务选项
- LiveData 记录下 +
- 依赖注入(DI),自动配置,集合注入
- - display image API OpenCV 】 【 imshow () to a depth (data type) at different image processing methods
- 【多线程进阶】--- 常见锁策略,CAS,synchronized底层工作原理,JUC,线程安全的集合类,死锁
- Postman will return to the interface to generate a json file to the local
- spark中的bykey
- 我国有关信息方面的法律法规
- Oracle Rac 集群文件目录迁移
- PMP每日一练 | 考试不迷路-8.2(包含敏捷+多选)
猜你喜欢
随机推荐
现货黄金分析的主要流派
SSM整合流程
Flink对比Spark
tmp
CDGA|如何加强数字政府建设?
HCIP笔记整理 2022/7/18
关于NOI 2022的报到通知
qt学习之旅--MinGW编译FFmpeg(32bit)
HCIP笔记整理 2022/7/20
(十四)51单片机——LCD1602实现滚动效果
hashSet解析
【云原生--Kubernetes】kubectl命令详解
c现代方法16章基础
CISP-PTE Zhenti Demonstration
升级
如何像用自来水一样使用数据库?|腾讯云数据库TDSQL-C
贷中存量客户的价值挖掘与分类实现,试试这一重要的场景模型
Nanny level explains Transformer
JS 原型原型链
海思项目总结









