📌
# 대륙별 spirit_servings의 평균, 최소, 최대, 합계를 출력
#1
# 평균
drinks.groupby("continent")["spirit_servings"].mean()
# 최소
drinks.groupby("continent")["spirit_servings"].min()
# 최대
drinks.groupby("continent")["spirit_servings"].max()
# 합계
drinks.groupby("continent")["spirit_servings"].sum()
#2
drinks.groupby("continent")["spirit_servings"].agg(["mean","min","max","sum"])
# 대륙별 알콜량의 평균이 전체 알콜량 평균보다 많은 대륙을 조회하기
# 전체 알콜량의 평균
#1
total_mean = drinks.total_litres_of_pure_alcohol.mean()
total_mean
#2
total_mean = drinks["total_litres_of_pure_alcohol"].mean()
total_mean
# 대륙별 알콜량의 평균
continent_mean = drinks.groupby('continent').mean()\\
['total_litres_of_pure_alcohol']
continent_mean
##
#1
over_mean = continent_mean[continent_mean > total_mean]
over_mean
list(over_mean.index)
#2
continent_mean[continent_mean > total_mean].index
# 대륙별 beer_servings 평균이 가장 많은 대륙 조회하기
drinks.groupby('continent').beer_servings.mean()
drinks.groupby('continent').beer_servings.mean().idxmax()
# 대륙별 beer_servings 평균이 가장 적은 대륙 조회하기
drinks.groupby('continent').beer_servings.mean().idxmin()
# 대륙별 total_litres_of_pure_alcohol 섭취량 평균 시각화 하기
import numpy as np
plt.rc("font", family="Malgun Gothic")
cont_mean = \\
drinks.groupby("continent")["total_litres_of_pure_alcohol"].mean()
cont_mean
#대륙명 : x축의 라벨
continents = cont_mean.index.tolist()
continents.append("Mean") #x축의 라벨 추가
x_pos = np.arange(len(continents)) #0~6까지 숫자
#y축 데이터 : 대륙별 평균값
alcohol = cont_mean.tolist()
alcohol
alcohol.append(total_mean) #전체 알콜 섭취 평균
#그래프화
#plt.bar : 막대그래프
#bar_list : 막대그래프안의 막대목록
bar_list =plt.bar(x_pos, alcohol, align='center', alpha=0.5)
bar_list #아무것도 출력안됨
#bar_list[len(continents) -1] : bar_list[6] 막대
#set_color('r') : 색상 설정. r:red
bar_list[len(continents) -1].set_color('r') #7번째 막대색 red로 설정
#plt.plot : 선그래프
#[0.,6] : x축 값
#[total_mean, total_mean] : y축의 값
#"k-" : 검정 실선. -- : 점선
plt.plot([0.,6], [total_mean, total_mean],"k-")
#x축의 값을 변경 : 0~6숫자를 continents의 내용 변경
plt.xticks(x_pos, continents)
plt.ylabel('total_litres_of_pure_alcohol') #y축 설명
plt.title('대륙별 알콜 섭취량') #제목
plt.show()
📌
'''
대륙별 beer_servings의 평균을 막대그래프로 시각화하기
가장 많은 맥주 소비하는 대륙(EU)의 막대의 색상을 빨강색("r")으로 변경하기
전체 맥주 소비량 평균을 구해서 막대 그래프에 추가
평균선을 출력하기.
평균 막대 색상은 노랑색("y")
평균선은 검정색("k--")
'''
import numpy as np
from matplotlib import rc
rc('font', family='Malgun Gothic') # 한글폰트
# 전체 맥주 소비량
total_beer = drinks.beer_servings.mean()
total_beer
# 대륙별 맥주 소비량 평균
beer_mean = drinks.groupby("continent")["beer_servings"].mean()
beer_mean
# 대륙명.
continents = beer_mean.index.tolist()
continents
continents.append("Mean") # 평균값 추가
x_pos = np.arange(len(continents))
beer = beer_mean.tolist()
beer.append(total_beer)
beer
# beer_mean의 최대값
beer_mean.max()
# beer_mean의 최대인덱스
beer_mean.idxmax()
# beer_mean 의 최대 인덱스 순번. 0부터 시작
beer_mean.argmax()
beer_mean.idxmin()
# continents Mean 데이터의 인덱스 순번
continents.index("Mean")
# 그래프화
# plt.bar : 막대 그래프
# bar_list : 막대그래프의 막대 목록
bar_list = plt.bar(x_pos, beer, align='center', alpha=0.5)
bar_list[beer_mean.argmax()].set_color('r')
bar_list[continents.index("Mean")].set_color('y')
# plt.plot : 선그래프
# [0.,6] : x축의 값목
# [total_mean, total_mean] : y축의 값
# "k-" : 검정색 실선. -- : 점선
plt.plot([0.,6], [total_beer, total_beer],"k--")
# x축의 값을 변경. : 0~6 숫자를 continent의 내용으로 변경
plt.xticks(x_pos, continents)
plt.ylabel('맥주 소비량') # y축 설명.
plt.title('대륙별 평균 맥주 소비량') # 제
plt.show()
'수업(국비지원) > Python' 카테고리의 다른 글
[Python] drinks.csv 파일 분석하기4 (0) | 2023.04.26 |
---|---|
[Python] drinks.csv 파일 분석하기3 (0) | 2023.04.26 |
[Python] drinks.csv 파일 분석하기1 (0) | 2023.04.26 |
[Python] chipo.tsc 파일 분석하기2 (0) | 2023.04.26 |
[Python] chipo.tsc 파일 분석하기1 (0) | 2023.04.26 |