CSV 읽기와 집계 수행
소규모부터 대규모까지 다양한 규모의 회사에서 일하는 Data Scientist의 급여 스프레드시트가 있습니다. 회사 규모별 평균 급여에 큰 차이가 있는지 확인해 보려고 합니다.
작업 공간에는 이미 SparkSession인 spark가 준비되어 있다는 점을 기억하세요!
이 연습은 강의의 일부입니다
PySpark 입문
연습 안내
- CSV 파일을 DataFrame으로 로드하고 스키마를 자동 추론하세요.
- 행의 총개수를 반환하세요.
company_size열로 그룹화하고salary_in_usd로 평균 급여를 계산하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Load the CSV file into a DataFrame
salaries_df = ____("salaries.csv", header=True, inferSchema=____)
# Count the total number of rows
row_count = salaries_df.____
print(f"Total rows: {row_count}")
# Group by company size and calculate the average of salaries
salaries_df.____("company_size").____({"salary_in_usd": "avg"}).show()
salaries_df.show()