python - 排序数据帧在连接两个数据帧时以无序方式返回数据

标签 python apache-spark pyspark

我有两个数据框playersstriker_detailsstriker_details df 按 Striker_grade 排序,如下所示:

+-------------+-------------+
|player_api_id|striker_grade|
+-------------+-------------+
|        20276|        89.25|
|        37412|         89.0|
|        38817|        88.75|
|        32118|        88.25|
|        31921|         87.0|
+-------------+-------------+ 

另一个数据框players是一个无序数据框,如下所示:

+---+-------------+------------------+------------------+-------------------+------+------+
| id|player_api_id|       player_name|player_fifa_api_id|           birthday|height|weight|
+---+-------------+------------------+------------------+-------------------+------+------+
|  1|       505942|Aaron Appindangoye|            218353|1992-02-29 00:00:00|182.88|   187|
|  2|       155782|   Aaron Cresswell|            189615|1989-12-15 00:00:00|170.18|   146|
|  3|       162549|       Aaron Doran|            186170|1991-05-13 00:00:00|170.18|   163|
|  4|        30572|     Aaron Galindo|            140161|1982-05-08 00:00:00|182.88|   198|
|  5|        23780|      Aaron Hughes|             17725|1979-11-08 00:00:00|182.88|   154|
+---+-------------+------------------+------------------+-------------------+------+------+

当我尝试使用 pyspark join 连接两个数据帧时,它返回无序数据:

+-------------+-----------------+------------------+-------------------+------+------+
|player_api_id|    striker_grade|       player_name|           birthday|height|weight|
+-------------+-----------------+------------------+-------------------+------+------+
|       309726|75.38888888888889|    Andrea Belotti|1993-12-20 00:00:00|180.34|   159|
|        38433|          72.5625|      Borja Valero|1985-01-12 00:00:00|175.26|   161|
|        41157|             82.0|Giovani dos Santos|1989-05-11 00:00:00|175.26|   163|
|        40740|           70.375|      Jeremy Morel|1984-04-02 00:00:00|172.72|   157|
|       109653|             73.5|     John Goossens|1988-07-25 00:00:00|175.26|   150|
+-------------+-----------------+------------------+-------------------+------+------+

我使用了命令:ss = Striker_details.join(players, ["player_api_id"], "inner")

如何实现数据排序?

最佳答案

尝试在 join 末尾链接 orderBy('striker_grade,ascending=False):

ss = striker_details.join(players, ["player_api_id"], "inner").orderBy('striker_grade',ascending=False)

ss.display()

此外,默认连接是inner,因此您无需指定它。不过,为了明确起见,最好将其显示出来。

关于python - 排序数据帧在连接两个数据帧时以无序方式返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69084411/

相关文章:

python - 列表中正值的 python 滚动窗口

python - 在 python 中使用字符串扩展切片

c++ - 谷歌机器人信息?

Python C Api 将 PyObject * 传输到 c 数组

java - PySpark SparkConf() 相当于 Spark 命令选项 "--jars"

apache-spark - 如何在 Spark 中过滤具有特定条件的数据帧

python - pyspark:Spark 2.3 中的 arrays_zip 等效项

python - pyspark dataframe cube 方法返回重复的空值

scala - Spark Dataset聚合类似于RDD aggregate(zero)(accum, combiner)

python - pyspark 多列条件并返回新列