我有两个数据框players
和striker_details
。 striker_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/