테이블 필터링을 위한 SQL 쿼리
이전 연습 문제에서는 DataFrame에 대해 간단한 SQL 쿼리를 실행해 보셨습니다. 원하는 결과를 얻고 이후의 데이터 시각화나 Machine Learning 같은 분석에 활용할 수 있도록 더 정교한 쿼리를 구성할 수도 있어요. 이 연습에서는 앞에서 생성한 임시 테이블 people을 사용하여 "sex"가 남성과 여성인 행을 각각 필터링해 두 개의 DataFrame을 만들겠습니다.
참고로, 이 "solution"은 SQL 명령의 대소문자를 구분합니다(예: FROM만 허용하고 from은 허용하지 않음). 또한 "solution"은 =가 아니라 "=="만 허용합니다.
워크스페이스에는 이미 SparkSession spark와 임시 테이블 people이 준비되어 있습니다.
이 연습은 강의의 일부입니다
PySpark로 배우는 빅데이터 기초
연습 안내
people테이블에서 sex가 female인 모든 행을 필터링하여people_female_dfDataFrame에 저장하세요.people테이블에서 sex가 male인 모든 행을 필터링하여people_male_dfDataFrame에 저장하세요.people_female와people_male두 DataFrame의 행 개수를 각각 세어 보세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Filter the people table to select female sex
people_female_df = spark.____('SELECT * FROM ____ WHERE sex=="____"')
# Filter the people table DataFrame to select male sex
people_male_df = spark.____('SELECT * ____ people ____ ____=="____"')
# Count the number of rows in both people_df_female and people_male_df DataFrames
print("There are {} rows in the people_female_df and {} rows in the people_male_df DataFrames".format(people_female_df.____(), people_male_df.____()))