切分並展開文字欄位
已提供一個具有 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: ", ____)