開始使用免費開始

切分租借費率

在影片練習中,你看過如何使用 pandas 來切分 film 資料表中的電子郵件地址欄位,以擷取使用者的網域名稱。現在,假設你想更了解使用者為電影支付的費率,因此你決定把 rental_rate 欄位分成美元與美分兩部分。

在這個練習中,你將用與影片練習相同的技巧來完成這件事!film 資料表已載入為 pandasDataFrame,名稱為 film_df。請記住,目標是把 rental_rate 欄位分拆成美元與美分。

本練習屬於課程

Data Engineering 入門

檢視課程

練習說明

  • 使用 .astype() 方法將 rental_rate 欄位轉成字串物件的欄位,並將結果指定給 rental_rate_str
  • '.' 來切分 rental_rate_str,並將結果展開為多個欄位。把結果指定給 rental_rate_expanded
  • 使用新建立的欄位名稱 rental_rate_dollarrental_rate_cents,將展開後的欄位指派回 films_df,並使用正確的索引設定對應的欄位。

動手互動練習

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

# Get the rental rate column as a string
rental_rate_str = film_df.____.____("____")

# Split up and expand the column
rental_rate_expanded = rental_rate_str.____.____("____", expand=True)

# Assign the columns to film_df
film_df = film_df.assign(
    ____=____[____],
    ____=____[____],
)
編輯並執行程式碼