pyspark 使用 window 函数
pyspark using window function
我有一个数据框,其中包含表示用户对特定电影的评级实例的行。每部电影可以由多个用户在多个类别中进行评分。这是我使用 movie_lens 数据创建的结果数据框。
|movie_id|year|categories|
+--------+----+----------+
| 122|1990| Comedy|
| 122|1990| Romance|
| 185|1990| Action|
| 185|1990| Crime|
| 185|1990| Thriller|
| 231|1990| Comedy|
| 292|1990| Action|
| 292|1990| Drama|
| 292|1990| Sci-Fi|
| 292|1990| Thriller|
| 316|1990| Action|
| 316|1990| Adventure|
| 316|1990| Sci-Fi|
| 329|1990| Action|
| 329|1990| Adventure|
| 329|1990| Drama|
.
.
.
movie_id是电影的唯一id,year是用户评价电影的年份,category是电影的12个类别之一。部分文件 here
我想在每个类别中的每个十年中找到评分最高的电影(计算每个类别中每个十年中每部电影的频率)
类似于
+-----------------------------------+
| year | category | movie_id | rank |
+-----------------------------------+
| 1990 | Comedy | 1273 | 1 |
| 1990 | Comedy | 6547 | 2 |
| 1990 | Comedy | 8973 | 3 |
.
.
| 1990 | Comedy | 7483 | 10 |
.
.
| 1990 | Drama | 1273 | 1 |
| 1990 | Drama | 6547 | 2 |
| 1990 | Drama | 8973 | 3 |
.
.
| 1990 | Comedy | 7483 | 10 |
.
.
| 2000 | Comedy | 1273 | 1 |
| 2000 | Comedy | 6547 | 2 |
.
.
for every decade, top 10 movies in each category
我了解需要使用 pyspark window 函数。这是我试过的
windowSpec = Window.partitionBy(res_agg['year']).orderBy(res_agg['categories'].desc())
final = res_agg.select(res_agg['year'], res_agg['movie_id'], res_agg['categories']).withColumn('rank', func.rank().over(windowSpec))
但它 returns 如下所示:
+----+--------+------------------+----+
|year|movie_id| categories|rank|
+----+--------+------------------+----+
|2000| 8606|(no genres listed)| 1|
|2000| 1587| Action| 1|
|2000| 1518| Action| 1|
|2000| 2582| Action| 1|
|2000| 5460| Action| 1|
|2000| 27611| Action| 1|
|2000| 48304| Action| 1|
|2000| 54995| Action| 1|
|2000| 4629| Action| 1|
|2000| 26606| Action| 1|
|2000| 56775| Action| 1|
|2000| 62008| Action| 1|
我是 pyspark 的新手,被困在这里。谁能指导我我做错了什么。
没错,您需要使用 window,但首先,您需要执行第一次聚合来计算频率。
首先,让我们计算十年。
df_decade = df.withColumn("decade", concat(substring(col("year"), 0, 3), lit("0")))
然后我们按十年、类别和movie_id计算频率:
agg_df = df_decade\
.groupBy("decade", "category", "movie_id")\
.agg(count(col("*")).alias("freq"))
最后,我们定义了一个 window 按十年和类别划分的 select 使用排名函数的前 10 名:
w = Window.partitionBy("decade", "category").orderBy(desc("freq"))
top10 = agg_df.withColumn("r", rank().over(w)).where(col("r") <= 10)
我有一个数据框,其中包含表示用户对特定电影的评级实例的行。每部电影可以由多个用户在多个类别中进行评分。这是我使用 movie_lens 数据创建的结果数据框。
|movie_id|year|categories|
+--------+----+----------+
| 122|1990| Comedy|
| 122|1990| Romance|
| 185|1990| Action|
| 185|1990| Crime|
| 185|1990| Thriller|
| 231|1990| Comedy|
| 292|1990| Action|
| 292|1990| Drama|
| 292|1990| Sci-Fi|
| 292|1990| Thriller|
| 316|1990| Action|
| 316|1990| Adventure|
| 316|1990| Sci-Fi|
| 329|1990| Action|
| 329|1990| Adventure|
| 329|1990| Drama|
.
.
.
movie_id是电影的唯一id,year是用户评价电影的年份,category是电影的12个类别之一。部分文件 here
我想在每个类别中的每个十年中找到评分最高的电影(计算每个类别中每个十年中每部电影的频率)
类似于
+-----------------------------------+
| year | category | movie_id | rank |
+-----------------------------------+
| 1990 | Comedy | 1273 | 1 |
| 1990 | Comedy | 6547 | 2 |
| 1990 | Comedy | 8973 | 3 |
.
.
| 1990 | Comedy | 7483 | 10 |
.
.
| 1990 | Drama | 1273 | 1 |
| 1990 | Drama | 6547 | 2 |
| 1990 | Drama | 8973 | 3 |
.
.
| 1990 | Comedy | 7483 | 10 |
.
.
| 2000 | Comedy | 1273 | 1 |
| 2000 | Comedy | 6547 | 2 |
.
.
for every decade, top 10 movies in each category
我了解需要使用 pyspark window 函数。这是我试过的
windowSpec = Window.partitionBy(res_agg['year']).orderBy(res_agg['categories'].desc())
final = res_agg.select(res_agg['year'], res_agg['movie_id'], res_agg['categories']).withColumn('rank', func.rank().over(windowSpec))
但它 returns 如下所示:
+----+--------+------------------+----+
|year|movie_id| categories|rank|
+----+--------+------------------+----+
|2000| 8606|(no genres listed)| 1|
|2000| 1587| Action| 1|
|2000| 1518| Action| 1|
|2000| 2582| Action| 1|
|2000| 5460| Action| 1|
|2000| 27611| Action| 1|
|2000| 48304| Action| 1|
|2000| 54995| Action| 1|
|2000| 4629| Action| 1|
|2000| 26606| Action| 1|
|2000| 56775| Action| 1|
|2000| 62008| Action| 1|
我是 pyspark 的新手,被困在这里。谁能指导我我做错了什么。
没错,您需要使用 window,但首先,您需要执行第一次聚合来计算频率。
首先,让我们计算十年。
df_decade = df.withColumn("decade", concat(substring(col("year"), 0, 3), lit("0")))
然后我们按十年、类别和movie_id计算频率:
agg_df = df_decade\
.groupBy("decade", "category", "movie_id")\
.agg(count(col("*")).alias("freq"))
最后,我们定义了一个 window 按十年和类别划分的 select 使用排名函数的前 10 名:
w = Window.partitionBy("decade", "category").orderBy(desc("freq"))
top10 = agg_df.withColumn("r", rank().over(w)).where(col("r") <= 10)