시작하기무료로 시작하기

스키마 작성

이제까지 여러 방법으로 스키마를 불러왔죠. 이번에는 스키마를 직접 정의해 보겠습니다. 다음의 데이터 사전을 사용할게요:

Variable Description
age 개인의 나이
education_num 학력(학위 수준)
marital_status 혼인 상태
occupation 직업
income 소득 범주형 변수

이 연습은 강의의 일부입니다

PySpark 입문

강의 보기

연습 안내

  • 데이터 스키마를 지정하세요. 열 이름은 (age,education_num,marital_status,occupation,income)이고, 각 열의 타입을 설정하세요. 파일 구분자는 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.____
코드 편집 및 실행