Split และ Explode คอลัมน์ข้อความ
มี DataFrame ชื่อ clauses_df ที่มี 100 แถวให้พร้อมใช้งานแล้ว โดยมีคอลัมน์ clause และ row id แต่ละ clause เป็น string ที่ประกอบด้วยคำตั้งแต่หนึ่งคำขึ้นไป โดยคั่นด้วยช่องว่าง
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Spark SQL in Python
คำแนะนำการฝึกหัด
- แยกคอลัมน์
clauseออกเป็นคอลัมน์ชื่อwordsที่เก็บ array ของคำแต่ละคำ - Explode คอลัมน์
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: ", ____)