청크로 데이터를 불러오는 이터레이터 작성 (2)
이전 연습 문제에서는 read_csv()를 사용해 큰 데이터셋에서 DataFrame 청크를 읽어 들였습니다. 이번 연습에서는 더 큰 DataFrame 청크 크기로 파일을 읽고 첫 번째 청크의 데이터를 처리해 보겠습니다.
데이터를 처리하기 위해 특정 국가의 행만으로 구성된 또 다른 DataFrame을 만듭니다. 그런 다음 새 DataFrame에서 'Total Population'과 'Urban population (% of total)' 두 열을 함께 zip으로 묶습니다. 마지막으로 zip 객체에서 각 튜플이 두 열 각각의 값 하나씩으로 이루어진 튜플 목록을 생성합니다.
현재 디렉터리에 있는 'ind_pop_data.csv' 데이터를 사용합니다. pandas는 pd로 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python 도구 상자
연습 안내
pd.read_csv()를 사용해'ind_pop_data.csv'파일을1000크기의 청크로 읽어들이세요. 결과를urb_pop_reader에 할당하세요.- 이터러블
urb_pop_reader에서 첫 번째 DataFrame 청크를 가져와df_urb_pop에 할당하세요. 'CountryCode'가'CEB'인df_urb_pop의 행만 선택하세요. 이를 위해df_urb_pop[____]의 대괄호 안에서df_urb_pop['CountryCode']가'CEB'와 같은지 비교하세요.zip()을 사용해df_pop_ceb의'Total Population'열과'Urban population (% of total)'열을 함께 zip으로 묶으세요. 결과 zip 객체를pops에 할당하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Initialize reader object: urb_pop_reader
urb_pop_reader = pd.read_csv(____, ____)
# Get the first DataFrame chunk: df_urb_pop
df_urb_pop = next(____)
# Check out the head of the DataFrame
print(df_urb_pop.head())
# Check out specific country: df_pop_ceb
df_pop_ceb = df_urb_pop[____]
# Zip DataFrame columns of interest: pops
pops = zip(____, ____)
# Turn zip object into list: pops_list
pops_list = list(pops)
# Print pops_list
print(pops_list)