開始使用免費開始

時間元件

能運用時間元件來建立特徵很重要,你也可以用它們來更深入探索與理解資料。在本練習中,你要檢視房屋在一週中的哪一天刊登是否有規律。請注意,PySpark 的一週是從星期日開始,數值為 1,到星期六結束,數值為 7。

本練習屬於課程

使用 PySpark 進行特徵工程

檢視課程

練習說明

  • pyspark.sql.functions 匯入 to_date()dayofweek()
  • 使用 to_date()LISTDATE 轉換為 Spark 的 date 型別,並用 withColumn() 就地覆寫該欄位。
  • 使用 LISTDATEdayofweek() 建立新欄位,並用 withColumn() 將其儲存為 List_Day_of_Week
  • 隨機抽樣一半的 DataFrame,接著用 toPandas() 轉成 pandas DataFrame,然後以 seaborn 的 countplot() 繪製此 pandas DataFrame 的 List_Day_of_Week 欄位分佈圖(x = List_Day_of_Week)。

動手互動練習

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

# Import needed functions
from ____ import ____, ____

# Convert to date type
df = df.____(____, ____(____))

# Get the day of the week
df = df.____(____, ____(____))

# Sample and convert to pandas dataframe
sample_df = df.sample(False, ____, 42).____()

# Plot count plot of of day of week
sns.____(x="List_Day_of_Week", data=____)
plt.show()
編輯並執行程式碼