当前位置: 首页 > 知识库问答 >
问题:

PySpark:desc函数在dataframe中的以下两种使用有什么不同?

长孙阳嘉
2023-03-14

empdf[“姓氏”].desc()desc(“姓氏”)有什么区别,因为两者都给出了相同的结果,而且都涉及到洗牌操作

>>> empDF.orderBy(empDF["Last Name"].desc()).show(4)
+------+----------+---------+------+------+
|Emp ID|First Name|Last Name|Gender|Salary|
+------+----------+---------+------+------+
|977421|   Zackary|  Zumwalt|     M|177521|
|741150|    Awilda|    Zuber|     F|144972|
|274620|  Eleanora|     Zook|     F|151026|
|242757|      Erin|     Zito|     F|127254|
+------+----------+---------+------+------+
only showing top 4 rows

>>> empDF.orderBy(desc("Last Name")).show(4)
+------+----------+---------+------+------+
|Emp ID|First Name|Last Name|Gender|Salary|
+------+----------+---------+------+------+
|977421|   Zackary|  Zumwalt|     M|177521|
|741150|    Awilda|    Zuber|     F|144972|
|274620|  Eleanora|     Zook|     F|151026|
|242757|      Erin|     Zito|     F|127254|
+------+----------+---------+------+------+
only showing top 4 rows

共有1个答案

宋博易
2023-03-14

两者都是一回事。根据文档和源代码(funtions.desc.)-

/**
   * Returns a sort expression based on the descending order of the column.
   * {{{
   *   df.sort(asc("dept"), desc("age"))
   * }}}
   *
   * @group sort_funcs
   * @since 1.3.0
   */
  def desc(columnName: String): Column = Column(columnName).desc

内部检查desc(columnName)调用列(columnName).desc,因此两者相同(将这两个选项作为执行相同操作的两个选项)

 类似资料: