当前位置:网站首页>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|
+-----------+---+------+
边栏推荐
猜你喜欢
随机推荐
C#操作FTP上传文件后检查上传正确性
界面仅允许扫码枪录入禁止手工键盘输入
MySql data format is converted to Redis key-value pair format
el-tree设置利用setCheckedNodessetCheckedKeys默认勾选节点,以及通过setChecked新增勾选指定节点
cnpm的安装与使用
超全!9种PCB表面处理工艺大对比
JUC并发编程深入浅出!
PCB制造常用的13种测试方法,你了解几种?
AQS、CAS、Synchronized小理解
记一次postgresql中使用正则表达式
ESXI主机给虚拟机添加USB加密狗设备
【Personal summary】Key points of MES system development/management
MySQL的DATE_FORMAT()函数将Date转为字符串
【项目案例】配置小型网络WLAN基本业务示例
【nohup】nohup命令的简单使用
Oracle 11g静默安装
MySQL 日期时间类型精确到毫秒
【GIoU loss】GIoU loss损失函数理解
Redis的应用详解
sql中 exists的用法









