開始使用免費開始

計算人氣的分位數

你正在持續分析包含 25 張熱門專輯的 Spotify 資料集,內含 streamsmonthly_listenerspopularity 等指標。行銷團隊需要明確的分級,來標示表現不佳、一般水準,以及爆紅的專輯。請計算分位數,協助團隊了解人氣分布情況。

polars 已以 pl 載入。含有串流指標的 DataFrame spotify 已為你預先載入。

本練習屬於課程

使用 Polars 進行資料轉換

檢視課程

練習說明

  • 選取 popularity 欄並計算第 25 百分位,將其命名為 q25_popularity
  • 另外加入第 50 與第 75 百分位,並使用相對應的別名。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Calculate the 25th, 50th, and 75th percentiles of popularity
result = spotify.select(
    pl.col("____").____(0.25).alias("q25_popularity"),
    pl.col("popularity").quantile(____).alias("q50_popularity"),
    pl.col("popularity").quantile(____).alias("q75_popularity"),
)

print(result)
編輯並執行程式碼