開始使用免費開始

不同分割區下的 ID

你剛完成在一個 DataFrame 中新增 ID 欄位。現在,來看看當你在分割區數量不同的 DataFrame 上做同樣的事,會發生什麼情況。

若要檢查分割區數量,請在 DataFrame 上使用 .rdd.getNumPartitions() 方法。

工作區中已提供 spark 工作階段,以及兩個 DataFrame:voter_dfvoter_df_single。依照說明操作,你會發現這兩個 DataFrame 的差異。pyspark.sql.functions 函式庫已以別名 F 匯入可用。

本練習屬於課程

使用 PySpark 清理資料

檢視課程

練習說明

  • 列印每個 DataFrame 的分割區數量。
  • 在每個 DataFrame 中新增 ROW_ID 欄位。
  • 顯示每個 DataFrame 前 10 筆 ID。

動手互動練習

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

# Print the number of partitions in each DataFrame
print("\nThere are %d partitions in the voter_df DataFrame.\n" % ____)
print("\nThere are %d partitions in the voter_df_single DataFrame.\n" % ____)

# Add a ROW_ID field to each DataFrame
voter_df = voter_df.____('ROW_ID', ____)
voter_df_single = ____

# Show the top 10 IDs in each DataFrame 
voter_df.____(voter_df.____.desc()).show(____)
____.orderBy(____).show(10)
編輯並執行程式碼