開始使用免費開始

切分並展開文字欄位

已提供一個具有 100 列的 dataframe clauses_df。它有一個 clause 欄位以及列編號。每個 clause 是以空格分隔的一個或多個單字所組成的字串。

本練習屬於課程

Python Spark SQL 入門

檢視課程

練習說明

  • clause 欄位切分為名為 words 的欄位,內容為各單字所組成的陣列。
  • words 欄位展開為名為 word 的欄位。
  • 統計結果列數。

動手互動練習

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

# Split the clause column into a column called words 
split_df = clauses_df.select(____('clause', ' ').____('words'))
split_df.show(5, truncate=False)

# Explode the words column into a column called word 
exploded_df = split_df.____(____('____').____('word'))
exploded_df.show(10)

# Count the resulting number of rows in exploded_df
print("\nNumber of rows: ", ____)
編輯並執行程式碼