Python - 연습문제 [엑셀 파일 불러오고 그래프 그리기 ]

# 1.시도별 전출입 인구수.xlsx 파일을 읽어 다음 그래프를 작성하기 
import pandas as pd 
import matplotlib.pyplot as plt 
df = pd.read_excel('시도별 전출입 인구수.xlsx', engine='openpyxl') 
# NA인 경우 앞의 값으로 채우기  
df = df.fillna(method="ffill") 
# 서울시에서 다른 지역으로 이주한 정보 선택 
mask=(df["전출지별"] == '서울특별시') & \ 
     (df["전입지별"] != '서울특별시') 
      
df_seoul = df[mask] 
print(df_seoul) 
# 전출지별 컬럼 삭제  
df_seoul = df_seoul.drop("전출지별",axis=1) 
# 전입지별 컬럼명 => 전입지 변경 
df_seoul.rename(columns={'전입지별':'전입지'}, inplace=True) 
# 전입지 컬럼을 인덱스명으로 설정  
df_seoul.set_index('전입지', inplace=True) 
# 한글 폰트로 설정 
from matplotlib import font_manager, rc 
font_name = font_manager.FontProperties\ 
    (fname="c:/Windows/Fonts/malgun.ttf").get_name() 
print(font_name) #폰트 이름 조회하는 명령어  
rc('font',family='Malgun Gothic') #폰트 이름 정확히 알고 있으면 해당 라인만 입력해도 됨 
# 3개 전출지 입력, 행렬을 전치한다. 
sr1 = df_seoul.loc[['전국','경기도','부산광역시']] 
sr2 = sr1.T 
print(sr2.head())  
# 그래프 그리기 
plt.style.use('ggplot') 
plt.figure(figsize=(14,5)) 
plt.xticks(rotation='vertical', size=10) 
plt.plot(sr2, marker='o', markersize=10) 
plt.title("서울 전출 인구의 지역별 이동") 
plt.xlabel('기간',size=20) 
plt.ylabel('이동 인구수',size=20) 
plt.legend(labels=[['전국','경기도','부산광역시']], loc='best')

# 2. sales_2015.xlsx 파일의 january_2015 sheet 을 읽어 Customer Name별 Sale Amount 를 
# 선그래프로 시각화 하기 
import pandas as pd 
import matplotlib.pyplot as plt 
infile = "sales_2015.xlsx" 
df = pd.read_excel(infile,"january_2015", index_col=None) 
df_value = df[["Customer Name","Sale Amount"]] 
df_value.set_index("Customer Name",inplace=True) 
print(df_value) 
plt.style.use("ggplot") 
plt.plot(df_value) 
plt.xlabel("Customer") 
plt.ylabel("Amount") 
plt.xticks(rotation=75,fontsizs="small") 
plt.title("Sales Amount") 
plt.show()