当前位置:网站首页>pyspark df 二次排序
pyspark df 二次排序
2022-08-03 05:29:00 【WGS.】
需求
根据两列排序,根据列1降序,当列1相同时根据列2降序
假数据
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:传入列表的时候,
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|
+-----------+---+------+
边栏推荐
猜你喜欢
随机推荐
在OracleLinux8.6的Zabbix6.0中监控Oracle11gR2
C#程序默认以管理员权限打开
Oracle 11g silent install
JumpServer如何传输文件以及复制剪切板
Content type ‘applicationx-www-form-urlencoded;charset=UTF-8‘ not supported“【已解决】
【云原生 · Kubernetes】Kubernetes基础环境搭建
postman配置中文
PostMan测试接口-----上传文件、导出excel
VO、DTO、DO、POJO的区别和概念
高密度 PCB 线路板设计中的过孔知识
WinServer2012r2破解多用户同时远程登录,并取消用户控制
一根网线完美解决IPTV+千兆网复用,还不来试试
单节点部署 gpmall 商城系统(一)
Oracle Common Commands - Basic Commands
JUC并发编程深入浅出!
使用Contab调用Shell脚本执行expdp自动备份Oracle
JDBC从手写连接到引用DBCP和C3P0
微信小程序 - 监听 TabBar 切换点击事件
el-table实现列筛选功能,控制列的显示和隐藏(实现简单,效果满分)
【FCOS】FCOS理论知识讲解








