開始使用免費開始

將欄位二值化

雖然數值型資料常常可以不做特徵工程就直接使用,但有時候進行一些轉換會更有幫助。舉例來說,有些情況你不在乎數值的大小,只在乎方向,或只是想知道是否存在。這時候就會想把欄位二值化。在 so_survey_df 資料中,有不少受訪者是自願工作(無薪)。你要建立一個名為 Paid_Job 的新欄位,用來表示每個人是否有領薪(水準大於 0)。

本練習屬於課程

Feature Engineering for Machine Learning in Python

檢視課程

練習說明

  • 建立一個名為 Paid_Job 的新欄位,先全部填入 0。
  • 將對應 ConvertedSalary 大於 0 的那些列,把 Paid_Job 的值改為 1。

動手互動練習

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

# Create the Paid_Job column filled with zeros
so_survey_df[____] = ____

# Replace all the Paid_Job values where ConvertedSalary is > 0
so_survey_df.____[____, 'Paid_Job'] = 1

# Print the first five rows of the columns
print(so_survey_df[['Paid_Job', 'ConvertedSalary']].head())
編輯並執行程式碼