시작하기무료로 시작하기

집계 값 표시하기

데이터를 더 집약된 형태로 표시해야 할 때가 있습니다. 예를 들어, co2_levels 데이터는 주간 단위 데이터이지만, 월별로 합쳐서 보여줘야 할 수도 있어요. 인덱스가 datetime 타입인 co2_levels 같은 DataFrame에서는 인덱스의 각 날짜에서 연도를 추출할 수 있습니다:

# df DataFrame 인덱스의 각 날짜에서 연도 추출
index_year = df.index.year

df DataFrame의 인덱스에 있는 날짜에서 월 또는 일을 추출하려면 각각 df.index.monthdf.index.day를 사용하면 됩니다. 이제 co2_levels DataFrame 인덱스에서 추출한 연도와 groupby 함수를 사용해 연도별 CO2 평균을 계산할 수 있어요:

df_by_year = df.groupby(index_year).mean()

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

Python으로 시계열 데이터 시각화

강의 보기

연습 안내

  • co2_levels DataFrame 인덱스의 각 날짜에서 월을 추출해 index_month라는 변수에 할당하세요.
  • pandas 라이브러리의 groupbymean 함수를 사용해 co2_levels의 월별 CO2 평균을 계산하고, 결과를 mean_co2_levels_by_month라는 새 DataFrame에 할당하세요.
  • mean_co2_levels_by_month DataFrame의 값을 플로팅하고, 축 눈금의 글꼴 크기는 6으로 설정하세요.

실습형 인터랙티브 연습

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

# Get month for each dates in the index of co2_levels
index_month = ____.index.____

# Compute the mean CO2 levels for each month of the year
mean_co2_levels_by_month = co2_levels.____(____).____()

# Plot the mean CO2 levels for each month of the year
mean_co2_levels_by_month.____

# Specify the fontsize on the legend
plt.legend(fontsize=10)

# Show plot
plt.show()
코드 편집 및 실행