ComenzarEmpieza gratis

Escritura del esquema

Ya hemos cargado esquemas de varias formas. Así que ahora definamos un esquema directamente. Usaremos un diccionario de datos:

Variable Descripción
age Edad de la persona
education_num Nivel educativo (por título)
marital_status Estado civil
occupation Ocupación
income Ingresos categóricos

Este ejercicio forma parte del curso

Introducción a PySpark

Ver curso

Instrucciones del ejercicio

  • Especifica el esquema de datos, indicando los nombres de las columnas (age, education_num, marital_status, occupation e income) y sus tipos, y establece una coma como separador en el argumento sep=.
  • Lee los datos de un archivo delimitado por comas llamado adult_reduced_100.csv.
  • Imprime el esquema del DataFrame resultante.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

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.____
Editar y ejecutar código