当前位置:网站首页>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|
+-----------+---+------+
边栏推荐
猜你喜欢
随机推荐
2022用户画像构建
【第1天】SQL快速入门-基础查询(SQL 小虚竹)
计算机网络常见面试题总结
关于任命韩文弢博士代理NOI科学委员会主席的公告
第六章:存储系统
static数据成员
学会可视化大屏布局技巧,让领导都赞不绝口
Shell脚本之一键安装mysql
华为设备配置BFD多跳检测
MySQL日期和时间戳的转换
【OpenCV】 - 显示图像API之imshow()对不同位深度(数据类型)的图像的处理方法
Roson的Qt之旅#106 QML在图片上方放置按钮并实现点击按钮切换图片
深入理解IO流(第一篇)
薛定谔的对象属性判断
华为设备配置BFD单跳检测二层链路
uniapp 请求接口封装
华为设备配置BFD状态与接口状态联动
volatile
Nanny level explains Transformer
REST学习









