rcl*_*mal 66 python dataframe apache-spark apache-spark-sql pyspark
我正在使用pyspark(Python 2.7.9/Spark 1.3.1)并且有一个数据帧GroupObject,我需要按降序对其进行过滤和排序.试图通过这段代码实现它.
group_by_dataframe.count().filter("`count` >= 10").sort('count', ascending=False)
Run Code Online (Sandbox Code Playgroud)
但它会引发以下错误.
sort() got an unexpected keyword argument 'ascending'
Run Code Online (Sandbox Code Playgroud)
zer*_*323 114
在PySpark 1.3中,sort方法不采用递增参数.您可以使用desc方法:
from pyspark.sql.functions import col
(group_by_dataframe
.count()
.filter("`count` >= 10")
.sort(col("count").desc()))
Run Code Online (Sandbox Code Playgroud)
或desc功能:
from pyspark.sql.functions import desc
(group_by_dataframe
.count()
.filter("`count` >= 10")
.sort(desc("count"))
Run Code Online (Sandbox Code Playgroud)
两种方法都可以与Spark> = 1.3(包括Spark 2.x)一起使用.
Hen*_*cio 62
使用orderBy:
group_by_dataframe.count().filter("`count` >= 10").orderBy('count', ascending=False)
Run Code Online (Sandbox Code Playgroud)
http://spark.apache.org/docs/2.0.0/api/python/pyspark.sql.html
gdo*_*ica 29
到目前为止,最方便的方法是使用这个:
df.orderBy(df.column_name.desc())
Run Code Online (Sandbox Code Playgroud)
不需要特殊的进口。
您也可以使用 groupBy 和 orderBy 如下
dataFrameWay = df.groupBy("firstName").count().withColumnRenamed("count","distinct_name").sort(desc("count"))
Run Code Online (Sandbox Code Playgroud)
小智 6
在 pyspark 2.4.4 中
1) group_by_dataframe.count().filter("`count` >= 10").orderBy('count', ascending=False)
2) from pyspark.sql.functions import desc
group_by_dataframe.count().filter("`count` >= 10").orderBy('count').sort(desc('count'))
Run Code Online (Sandbox Code Playgroud)
无需导入 1) 和 1) 简短易读,
所以我更喜欢 1) 而不是 2)