是否有推荐的方法在 pyspark 中实现分类数据的自定义排序?我理想地寻找 pandas 分类数据类型提供的功能。
因此,给定一个数据集Speed
列,可能的选项是["Super Fast", "Fast", "Medium", "Slow"]
。我想实现适合上下文的自定义排序。
如果我使用默认排序,类别将按字母顺序排序。 Pandas 允许将列数据类型更改为绝对的定义的一部分给出了自定义排序顺序:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Categorical.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Categorical.html
您可以使用orderBy
并使用定义您的自定义排序when
:
from pyspark.sql.functions import 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)
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)