pyspark数据帧中的自定义排序

编程入门 行业动态 更新时间:2024-10-14 10:45:43
本文介绍了pyspark数据帧中的自定义排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否存在用于在pyspark中对分类数据实现自定义排序顺序的任何推荐方法?理想情况下,我正在寻找pandas类别数据类型提供的功能.

Are there any recommended methods for implementing custom sort ordering for categorical data in pyspark? I'm ideally looking for the functionality the pandas categorical data type offers.

因此,给定具有 Speed 列的数据集,可能的选项为 [超快速",快速",中",慢"] .我想实现适合上下文的自定义排序.

So, given a dataset with a Speed column, the possible options are ["Super Fast", "Fast", "Medium", "Slow"]. I want to implement custom sorting that will fit the context.

如果我使用默认排序,则类别将按字母顺序排序.Pandas允许将列数据类型更改为分类,并且部分定义提供了自定义排序顺序: pandas.pydata/pandas-docs/stable/reference/api/pandas.Categorical.html

If I use the default sorting the categories will be sorted alphabetically. Pandas allows to change the column data type to be categorical and part of the definition gives a custom sort order: pandas.pydata/pandas-docs/stable/reference/api/pandas.Categorical.html

推荐答案

您可以使用 orderBy 并使用 when 时定义自定义顺序:

You can use orderBy and define your custom ordering using when:

from pyspark.sql.functions col, when df.orderBy(when(col("Speed") == "Super Fast", 1) .when(col("Speed") == "Fast", 2) .when(col("Speed") == "Medium", 3) .when(col("Speed") == "Slow", 4) )

更多推荐

pyspark数据帧中的自定义排序

本文发布于:2023-11-21 23:43:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   数据   pyspark

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!