Project Jupyter

Project Jupyter(지역소득 공공데이터)_2024-06-17

앵보몬 2025. 2. 26. 21:18
728x90
반응형

요구사항

1) 지역별 소득분포를 그래프로 도출하시오.

2) CSV 파일에서 개인소득, 민간소비, 지역내총생산, 지역총소득 데이터를 읽어오세요.

3) matplotlib을 사용하여 기준년도를 x축, 소득을 y축으로 하는 선 그래프를 그리세요.

4) 그래프 제목, x축 라벨, y축 라벨을 적절히 설정하세요.

 

예외 처리

1) 설명 : 파일 입출력 및 잘못된 입력에 대한 예외 처리

2) 사례 : 파일이 없을 경우, 잘못된 수량 입력

3) 핵심 : 안정적인 프로그램 동작 보장

 

import csv
import numpy as np
import matplotlib.pyplot as plt

while True:
    print("============= 지역별 소득통계 =============")
    print("1. 지역별 1인당 지역내총생산 조회")
    print("2. 지역별 1인당 지역총소득 조회")
    print("3. 지역별 1인당 개인소득 조회")
    print("4. 지역별 1인당 민간소비 조회")
    print("5. 프로그램 종료")
    print("===========================================")
    choice = int(input("선택 : "))
    
    if choice == 1:
        f = open(r'D:\web\python_work\2024-06-19\지역내총생산.csv')
        data = csv.reader(f)
        next(data)
        next(data)
        
        name = input("지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city = None
        for row in data:
            if name in row[0]:
                if '' in row:
                    continue
                city = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city is None:
            print(f"{name}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Re-open the file to get data for the second region
        f.seek(0)  # Reset the file pointer to the beginning
        next(data)
        next(data)
        
        name1 = input("두 번째 지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city1 = None
        for row in data:
            if name1 in row[0]:
                if '' in row:
                    continue
                city1 = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city1 is None:
            print(f"{name1}에 해당하는 데이터를 찾을 수 없습니다.")
            continue
        
        # Plot the data
        plt.style.use('ggplot')
        plt.figure(figsize=(10, 6))
        plt.rc('font', family='Malgun Gothic')
        plt.title(f'{name}와 {name1} 1인당 지역내총생산')
        plt.xlabel("기준년도")
        plt.ylabel("1인당 지역내총생산")
        plt.plot(city, 'hotpink', label=f'{name}')
        plt.plot(city1, 'skyblue', label=f'{name1}')
        plt.legend()
        plt.show()

    elif choice == 2:
        f = open(r'D:\web\python_work\2024-06-19\지역총소득.csv')
        data = csv.reader(f)
        next(data)
        next(data)

        name = input("지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city = None
        for row in data:
            if name in row[0]:
                if '' in row:
                    continue
                city = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city is None:
            print(f"{name}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Re-open the file to get data for the second region
        f.seek(0)
        next(data)
        next(data)

        name1 = input("두 번째 지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city1 = None
        for row in data:
            if name1 in row[0]:
                if '' in row:
                    continue
                city1 = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city1 is None:
            print(f"{name1}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Plot the data
        plt.style.use('ggplot')
        plt.figure(figsize=(10, 6))
        plt.rc('font', family='Malgun Gothic')
        plt.title(f'{name}와 {name1} 1인당 지역총소득')
        plt.xlabel("기준년도")
        plt.ylabel("1인당 지역총소득")
        plt.plot(city, 'hotpink', label=f'{name}')
        plt.plot(city1, 'skyblue', label=f'{name1}')
        plt.legend()
        plt.show()

    elif choice == 3:
        f = open(r'D:\web\python_work\2024-06-19\개인소득.csv')
        data = csv.reader(f)
        next(data)
        next(data)

        name = input("지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city = None
        for row in data:
            if name in row[0]:
                if '' in row:
                    continue
                city = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city is None:
            print(f"{name}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Re-open the file to get data for the second region
        f.seek(0)
        next(data)
        next(data)

        name1 = input("두 번째 지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city1 = None
        for row in data:
            if name1 in row[0]:
                if '' in row:
                    continue
                city1 = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city1 is None:
            print(f"{name1}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Plot the data
        plt.style.use('ggplot')
        plt.figure(figsize=(10, 6))
        plt.rc('font', family='Malgun Gothic')
        plt.title(f'{name}와 {name1} 1인당 개인소득')
        plt.xlabel("기준년도")
        plt.ylabel("1인당 개인소득")
        plt.plot(city, 'hotpink', label=f'{name}')
        plt.plot(city1, 'skyblue', label=f'{name1}')
        plt.legend()
        plt.show()

    elif choice == 4:
        f = open(r'D:\web\python_work\2024-06-19\민간소비.csv')
        data = csv.reader(f)
        next(data)
        next(data)

        name = input("지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city = None
        for row in data:
            if name in row[0]:
                if '' in row:
                    continue
                city = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city is None:
            print(f"{name}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Re-open the file to get data for the second region
        f.seek(0)
        next(data)
        next(data)

        name1 = input("두 번째 지역의 이름(시도별을 기준으로)을 입력해주세요 : ")
        city1 = None
        for row in data:
            if name1 in row[0]:
                if '' in row:
                    continue
                city1 = np.array([int(x.replace(',', '')) for x in row[1:]], dtype=int)
                break

        if city1 is None:
            print(f"{name1}에 해당하는 데이터를 찾을 수 없습니다.")
            continue

        # Plot the data
        plt.style.use('ggplot')
        plt.figure(figsize=(10, 6))
        plt.rc('font', family='Malgun Gothic')
        plt.title(f'{name}와 {name1} 1인당 민간소비')
        plt.xlabel("기준년도")
        plt.ylabel("1인당 민간소비")
        plt.plot(city, 'hotpink', label=f'{name}')
        plt.plot(city1, 'skyblue', label=f'{name1}')
        plt.legend()
        plt.show()

    elif choice == 5:
        print('프로그램을 종료합니다.')
        break
    else:
        print("잘못된 선택입니다. 다시 선택해 주세요.")

 

지역내총생산.csv
0.01MB
지역총소득.csv
0.00MB
민간소비.csv
0.00MB
개인소득.csv
0.00MB

728x90
반응형