开始使用免费开始使用

编写 Schema

我们已经用多种方式加载过 Schema。现在请直接定义一个 schema。我们将使用一个 数据字典:

变量 说明
age 个体年龄
education_num 受教育年限(按学历换算)
marital_status 婚姻状况
occupation 职业
income 收入类别

本练习是课程的一部分

PySpark 入门

查看课程

练习说明

  • 指定数据 schema,给出列名(ageeducation_nummarital_statusoccupationincome)及其列类型,并为 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.____
编辑并运行代码