code:
mydf = testDF.groupBy(testDF.word).count()
mydf.show()
output:
+-----------+-----+
| word|count|
+-----------+-----+
| she| 2208|
| mothers| 93|
| poet| 59|
| moving| 18|
| active| 6|
| foot| 169|
我想根据字数按降序排序此数据框。
code:
countDF = mydf.orderBy(mydf.count.desc())
countDF.show()
Error:
AttributeError: 'function' object has no attribute 'desc'
请告诉我我哪里出错了。
答案 0 :(得分:4)
嗯,点符号不是访问列的最佳方法。虽然DataFrame
提供了列感知__getattr__
,但您可能会遇到类似这样的冲突,其中name将解析为方法(此处为DataFrame.count
),因此最好使用括号表示法:
mydf.orderBy(mydf["count"].desc())
或col
功能:
from pyspark.sql.functions import col
mydf.orderBy(col("count").desc())
引用列。