始める無料で始める

スキーマの書き出し

これまでさまざまな方法でスキーマを読み込んできました。今度はスキーマを直接定義してみましょう。以下のデータ辞書を参考にしてください。

変数名 説明
age 年齢
education_num 学歴(学位別)
marital_status 婚姻状況
occupation 職業
income 収入カテゴリ

この演習はコースの一部です

PySpark 入門

コースを見る

演習の手順

  • データスキーマを定義し、列名(ageeducation_nummarital_statusoccupationincome)と列の型を指定してください。また、sep= 引数にカンマを設定してください。
  • adult_reduced_100.csv というカンマ区切りファイルからデータを読み込みます。
  • 作成した DataFrame のスキーマを表示してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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.____
コードを編集して実行