เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

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: ", ____)
แก้ไขและรันโค้ด