当前位置:网站首页>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|
+-----------+---+------+
边栏推荐
- C语言实现树的底层遍历--超简代码
- drop database出现1010
- RHCSA第四天
- The ORB - SLAM2 extracting feature points
- 标准输入流
- Example of embedding code for continuous features
- El - tree set using setCheckedNodessetCheckedKeys default check nodes, and a new check through setChecked specified node
- 华为设备配置BFD状态与接口状态联动
- excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
- 关于利用canvas画带箭头的直线旋转
猜你喜欢
RHCSA第四天
分布式数据库数据一致性的原理、与技术实现方案
《多线程案例》阻塞队列、定时器、线程池、饿汉与懒汉模式
解读 refresh 十二步骤
pt-online-schema-change工具使用的一次
1066 Root of AVL Tree // AVL平衡二叉搜索树模板
excel高级绘图技巧100讲(二十一)- Excel层叠柱形图
【着色器实现HandDrawn简笔画抖动效果_Shader效果第十二篇】
- display image API OpenCV 】 【 imshow () to a depth (data type) at different image processing methods
伦敦银现货市场如何使用多条均线?
随机推荐
pt-online-schema-change工具使用的一次
JS 原型原型链
ViewModel 记录下 +
华为设备配置BFD多跳检测
计算机网络常见面试题总结
Shell脚本之一键安装mysql
ISIJ 2022收官,中国初中生再展风采
Nanny level explains Transformer
CDGA|如何加强数字政府建设?
HCIP笔记整理 2022/7/20
数仓埋点体系与归因实践
- display image API OpenCV 】 【 imshow () to a depth (data type) at different image processing methods
ORB-SLAM2提取特征点
解决登录vCenter提示“当前网站安全证书不受信任“
Data warehouse buried point system and attribution practice
帆软11版本参数联动为null查询全部
一文搞懂什么是@Component和@Bean注解以及如何使用
【云原生--Kubernetes】Pod容器与镜像拉取策略
IEEE RAL投初稿
海思项目总结