一次替換多個樣式
分析團隊需要計算產品的平均容量,但 size 欄位包含像「30ml」這樣的文字,而不是數字。你可以一次替換多個樣式來清理這些值。
本練習屬於課程
使用 Polars 進行資料轉換
練習說明
- 將
"size"欄位中的"30ml"、"50ml"、"100ml"分別替換成其數值等價的"30"、"50"、"100"。 - 將
"size"欄位轉換(cast)為 64 位元整數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Replace size values with numeric strings
reviews.with_columns(
pl.col("size").str.____(["30ml","50ml","100ml"],["____","____","____"])
# Cast size column to integer
).____({"____":pl.Int64})