開始使用免費開始

計算花費四分位數(q=4)

我們已為你建立一個包含隨機 CustomerIDSpend 值的資料集作為 data。你現在要用這個資料集,依照 Spend 值把顧客分成四分位數,並為每一組指派標籤。

pandas 函式庫已載入為 pd。你可以自由在主控台列印 data

本練習屬於課程

Python 的客群分群

檢視課程

練習說明

  • 建立一個包含 4 組的花費四分位數——範圍介於 1 到 5。
  • 將四分位數的值指定到 dataSpend_Quartile 欄位。
  • 依照 Spend 值排序 data,並列印結果。

動手互動練習

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

# Create a spend quartile with 4 groups - a range between 1 and 5
spend_quartile = pd.____(data['Spend'], q=____, labels=range(1,____))

# Assign the quartile values to the Spend_Quartile column in data
data['____'] = spend_quartile

# Print data with sorted Spend values
print(____.sort_values(____))
編輯並執行程式碼