Definirea schemei
Am încărcat scheme în mai multe moduri până acum. Hai să definim o schemă direct. Vom folosi un dicționar de date:
| Variabilă | Descriere |
|---|---|
| age | Vârsta persoanei |
| education_num | Nivelul de educație |
| marital_status | Starea civilă |
| occupation | Ocupația |
| income | Venitul (categorial) |
Acest exercițiu face parte din cursul
Introducere în PySpark
Instrucțiuni pentru exercițiu
- Specifică schema de date, indicând numele coloanelor (
age,education_num,marital_status,occupationșiincome) și tipurile acestora, setând o virgulă pentru argumentulsep=. - Citește datele dintr-un fișier delimitat prin virgulă numit
adult_reduced_100.csv. - Afișează schema DataFrame-ului rezultat.
Exercițiu interactiv practic
Încearcă acest exercițiu completând acest cod de exemplu.
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.____