使用 PySpark 清理資料
Mike Metzger
Data Engineering Consultant
資料清理:為資料處理流程準備原始資料。
資料清理可能包含:
典型資料系統的問題:
Spark 的優勢:
原始資料:
| name | age (years) | city |
|---|---|---|
| Smith, John | 37 | Dallas |
| Wilson, A. | 59 | Chicago |
| null | 215 |
清理後資料:
| last name | first name | age (months) | state |
|---|---|---|---|
| Smith | John | 444 | TX |
| Wilson | A. | 708 | IL |
匯入結構描述
import pyspark.sql.types
peopleSchema = StructType([
# Define the name field
StructField('name', StringType(), True),
# Add the age field
StructField('age', IntegerType(), True),
# Add the city field
StructField('city', StringType(), True)
])
讀取包含資料的 CSV 檔
people_df = spark.read.format('csv').load(name='rawdata.csv', schema=peopleSchema)
使用 PySpark 清理資料