將串流數轉換為十億單位
你是唱片公司的資料分析師。你有一個 Spotify 資料集,包含 25 張熱門專輯,以及總播放次數、每月聽眾與熱門度分數等串流指標。streams 欄位以巨大整數顯示總播放次數,不易閱讀,所以你需要用自訂的 Python 函式把它們轉換成以十億為單位。
已將 polars 載入為 pl。spotify DataFrame 與 to_billions() 函式已為你預先載入。
本練習屬於課程
使用 Polars 進行資料轉換
練習說明
- 將
to_billions套用到streams欄位,建立名為streams_billions的新欄位。 - 將回傳型別明確指定為
pl.Float64,以進行型別控制。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Apply to_billions to the streams column
result = spotify.with_columns(
pl.col("streams").____(
____, return_dtype=____
).alias("streams_billions")
)
print(result.head())