通过PySpark中的几列从groupby获取具有最大值的行

Iva*_*van 3 python apache-spark pyspark

我有一个类似于的数据框

from pyspark.sql.functions import avg, first

rdd = sc.parallelize(
[
(0, "A", 223,"201603", "PORT"), 
(0, "A", 22,"201602", "PORT"), 
(0, "A", 22,"201603", "PORT"), 
(0, "C", 22,"201605", "PORT"), 
(0, "D", 422,"201601", "DOCK"), 
(0, "D", 422,"201602", "DOCK"), 
(0, "C", 422,"201602", "DOCK"), 
(1,"B", 3213,"201602", "DOCK"), 
(1,"A", 3213,"201602", "DOCK"), 
(1,"C", 3213,"201602", "PORT"), 
(1,"B", 3213,"201601", "PORT"), 
(1,"B", 3213,"201611", "PORT"), 
(1,"B", 3213,"201604", "PORT"), 
(3,"D", 3999,"201601", "PORT"), 
(3,"C", 323,"201602", "PORT"), 
(3,"C", 323,"201602", "PORT"), 
(3,"C", 323,"201605", "DOCK"), 
(3,"A", 323,"201602", "DOCK"), 
(2,"C", 2321,"201601", "DOCK"),
(2,"A", 2321,"201602", "PORT")
]
)
df_data = sqlContext.createDataFrame(rdd, ["id","type", "cost", "date", "ship"])
Run Code Online (Sandbox Code Playgroud)

而且我需要按id和进行汇总,以type使ship每个组的出现率最高。例如,

grouped = df_data.groupby('id','type', 'ship').count()
Run Code Online (Sandbox Code Playgroud)

有一个列,其中包含每个组的次数:

+---+----+----+-----+
| id|type|ship|count|
+---+----+----+-----+
|  3|   A|DOCK|    1|
|  0|   D|DOCK|    2|
|  3|   C|PORT|    2|
|  0|   A|PORT|    3|
|  1|   A|DOCK|    1|
|  1|   B|PORT|    3|
|  3|   C|DOCK|    1|
|  3|   D|PORT|    1|
|  1|   B|DOCK|    1|
|  1|   C|PORT|    1|
|  2|   C|DOCK|    1|
|  0|   C|PORT|    1|
|  0|   C|DOCK|    1|
|  2|   A|PORT|    1|
+---+----+----+-----+
Run Code Online (Sandbox Code Playgroud)

我需要得到

+---+----+----+-----+
| id|type|ship|count|
+---+----+----+-----+
|  0|   D|DOCK|    2|
|  0|   A|PORT|    3|
|  1|   A|DOCK|    1|
|  1|   B|PORT|    3|
|  2|   C|DOCK|    1|
|  2|   A|PORT|    1|
|  3|   C|PORT|    2|
|  3|   A|DOCK|    1|
+---+----+----+-----+
Run Code Online (Sandbox Code Playgroud)

我试图结合使用

grouped.groupby('id', 'type', 'ship')\
.agg({'count':'max'}).orderBy('max(count)', ascending=False).\
groupby('id', 'type', 'ship').agg({'ship':'first'})
Run Code Online (Sandbox Code Playgroud)

但是失败了。有没有办法从一组的计数中获取最大的行?

这个在大熊猫上的工作是:

df_pd = df_data.toPandas()
df_pd_t = df_pd[df_pd['count'] == df_pd.groupby(['id','type', ])['count'].transform(max)]
Run Code Online (Sandbox Code Playgroud)

mto*_*oto 5

根据您的预期输出,似乎您只是按id和分组ship-因为您已经有不同的值grouped-并因此基于和删除了重复的元素idship并按count排序type

为此,我们可以使用以下Window功能:

from pyspark.sql.window import Window
from pyspark.sql.functions import rank, col

window = (Window
          .partitionBy(grouped['id'],
                       grouped['ship'])
          .orderBy(grouped['count'].desc(), grouped['type']))


(grouped
 .select('*', rank()
         .over(window)
         .alias('rank')) 
  .filter(col('rank') == 1)
  .orderBy(col('id'))
  .dropDuplicates(['id', 'ship', 'count'])
  .drop('rank')
  .show())
+---+----+----+-----+
| id|type|ship|count|
+---+----+----+-----+
|  0|   D|DOCK|    2|
|  0|   A|PORT|    3|
|  1|   A|DOCK|    1|
|  1|   B|PORT|    3|
|  2|   C|DOCK|    1|
|  2|   A|PORT|    1|
|  3|   A|DOCK|    1|
|  3|   C|PORT|    2|
+---+----+----+-----+
Run Code Online (Sandbox Code Playgroud)