寫出 Schema
我們已經用多種方式載入過 Schema。現在直接定義一個 schema。以下是資料字典:
| 變數 | 說明 |
|---|---|
| age | 個人年齡 |
| education_num | 以學位表示的教育程度 |
| marital_status | 婚姻狀況 |
| occupation | 職業 |
| income | 分類型的收入 |
本練習屬於課程
PySpark 入門
練習說明
- 指定資料 schema,提供欄位名稱(
age、education_num、marital_status、occupation、income)與欄位型別,並將sep=參數設為逗號。 - 從逗號分隔的檔案
adult_reduced_100.csv讀取資料。 - 列印產生的 DataFrame 之 schema。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from pyspark.sql.types import StructType, StructField, IntegerType, StringType
# Fill in the schema with the columns you need from the exercise instructions
schema = StructType([____("____",____()),
____("____",____()),
____("marital_status",StringType()),
StructField("____",____()),
____("____",____()),
])
# Read in the CSV, using the schema you defined above
census_adult = spark.read.csv("adult_reduced_100.csv", sep='____', header=False, schema=schema)
# Print out the schema
census_adult.____