지난달 평균과 작년 평균의 비율
basetable에 추가하면 유용한 변수 중 하나는 기부자가 지난달에 기부한 평균 금액을 작년에 기부한 평균 금액과 비교한 값이에요. 이 연습 문제에서는 이 변수를 basetable에 추가하는 방법을 배워요. 지난달의 기부는 이미 gifts_last_month에, 작년의 기부는 gifts_last_year에 선택되어 있어요.
이 연습은 강의의 일부입니다
Python 중급 예측 분석
연습 안내
gifts_last_month에서 각 기부자에 대해 지난달 평균 기부액을 계산하세요.gifts_last_year에서 각 기부자에 대해 작년 평균 기부액을 계산하세요.- 지난달 평균 기부액과 작년 평균 기부액을 basetable에 추가하세요.
- basetable에서 지난달 평균과 작년 평균 기부액의 비율을 계산하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Average gift last month for each donor
average_gift_last_month = gifts_last_month.____("____")["____"].____().reset_index()
average_gift_last_month.columns = ["donor_ID", "mean_gift_last_month"]
# Average gift last year for each donor
average_gift_last_year = ____
average_gift_last_year.columns = ["donor_ID", "mean_gift_last_year"]
# Add average gift last month and year to basetable
basetable = pd.merge(____, ____, on="____", how="____")
basetable = pd.merge(____, ____, on="____", how="____")
# Calculate ratio of last month's and last year's average
basetable["ratio_month_year"] = basetable["____"] / basetable["____"]
print(basetable.head())