嗨,我想實作這樣的目標
SAS SQL: select * from flightData2015 group by DEST_COUNTRY_NAME order by count
我的資料如下所示:

這是我的火花代碼:
flightData2015.selectExpr("*").groupBy("DEST_COUNTRY_NAME").orderBy("count").show()
我收到了這個錯誤:
AttributeError:“GroupedData”物件沒有屬性“orderBy”。我是 pyspark 的新手。Pyspark的groupby和orderby和SAS SQL不一樣?
我也嘗試排序flightData2015.selectExpr("*").groupBy("DEST_COUNTRY_NAME").sort("count").show(),我收到了同樣的錯誤。"AttributeError: 'GroupedData' 物件沒有屬性 'sort'" 請幫忙!
uj5u.com熱心網友回復:
如果您想要每一行,則不需要分組。您可以按多列排序。
from pyspark.sql import functions as F
vals = [("United States", "Angola",13), ("United States","Anguilla" , 38), ("United States","Antigua", 20), ("United Kingdom", "Antigua", 22), ("United Kingdom","Peru", 50), ("United Kingdom", "Russisa",13), ("Argentina", "United Kingdom",13),]
cols = ["destination_country_name","origin_conutry_name", "count"]
df = spark.createDataFrame(vals, cols)
#display(df.orderBy(['destination_country_name', F.col('count').desc()])) If you want count to be descending
display(df.orderBy(['destination_country_name', 'count']))
uj5u.com熱心網友回復:
在 Spark 中,groupBy回傳一個GroupedData,而不是 DataFrame。通常,您總是在groupBy. 在這種情況下,即使 SAS SQL 沒有任何聚合,您仍然必須定義一個(如果需要,可以稍后洗掉)。
(flightData2015
.groupBy("DEST_COUNTRY_NAME")
.count() # this is the "dummy" aggregation
.orderBy("count")
.show()
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/437128.html
標籤:排序 pyspark 通过...分组 sql-order-by
上一篇:為什么sort(withkey)功能不能按預期作業?[復制]
下一篇:時間段內排名前10的主機/IP
