시작하기무료로 시작하기

DataFrame 필터링하기

이전 연습 문제에서는 주로 열 단위로 DataFrame을 부분 선택하는 select() 연산자를 사용했어요. 그럼 조건에 따라 DataFrame을 부분 선택하려면 어떻게 할까요? (예: 성별이 Female인 모든 행 선택) 이번 연습에서는 people_df DataFrame에서 'sex'가 female인 행과 male인 행을 각각 필터링하여 두 개의 데이터셋을 만들고, 마지막으로 각 데이터셋의 행 수를 세어 볼 거예요.

워크스페이스에는 이미 SparkSession spark와 DataFrame people_df가 준비되어 있어요.

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

PySpark로 배우는 빅데이터 기초

강의 보기

연습 안내

  • people_df DataFrame에서 성별이 female인 모든 행을 필터링하여 people_df_female DataFrame에 저장하세요.
  • people_df DataFrame에서 성별이 male인 모든 행을 필터링하여 people_df_male DataFrame에 저장하세요.
  • people_df_femalepeople_df_male DataFrame의 행 개수를 각각 세세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Filter people_df to select females 
people_df_female = people_df.____(people_df.____ == "female")

# Filter people_df to select males
people_df_male = people_df.____(____ == "____")

# Count the number of rows 
print("There are {} rows in the people_df_female DataFrame and {} rows in the people_df_male DataFrame".format(people_df_female.____(), people_df_male.____()))
코드 편집 및 실행