CSV 데이터 읽어오기¶
In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import csv
In [2]:
#
df = pd.read_csv('GDP2.csv', encoding = 'cp949')
df.head(100)
df= df.dropna(axis=1)
df.head()
Out[2]:
시점 | 대한민국 | 아프가니스탄 | 아르메니아 | 아제르바이잔 | 바레인 | 방글라데시 | 부탄 | 브루나이 | 캄보디아 | ... | 뉴칼레도니아 | 뉴질랜드 | 북마리아나제도 | 팔라우 | 파푸아뉴기니 | 사모아 | 솔로몬제도 | 통가 | 투발루 | 바누아투 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1995 | 9.6 | - | 6.9 | -11.8 | 3.9 | 5.1 | 7.1 | 4.5 | 9.9 | ... | 5.9 | 4.7 | - | - | -3.3 | 6.7 | 10.1 | 7.4 | -5.0 | 1.0 |
1 | 1996 | 7.9 | - | 5.9 | 1.3 | 4.1 | 4.5 | 5.6 | 2.9 | 5.9 | ... | 0.4 | 3.6 | - | - | 7.7 | 7.2 | 1.6 | 1.8 | -6.0 | 2.3 |
2 | 1997 | 6.2 | - | 3.3 | 5.8 | 3.1 | 4.5 | 5.4 | -1.5 | 4.0 | ... | 2.0 | 2.0 | - | - | -3.9 | 0.6 | -0.9 | 1.2 | 10.0 | 4.9 |
3 | 1998 | -5.1 | - | 7.3 | 10.0 | 4.8 | 5.2 | 5.9 | -0.6 | 4.7 | ... | -3.2 | 0.8 | - | - | -3.8 | 2.2 | 1.3 | 2.5 | 15.5 | 1.2 |
4 | 1999 | 11.5 | - | 3.3 | 7.4 | 4.3 | 4.7 | 8.0 | 3.1 | 12.7 | ... | 0.9 | 5.5 | - | - | 1.9 | 2.2 | -0.5 | 3.7 | -1.6 | 0.3 |
5 rows × 207 columns
세계 국가들 중 주요 국가 데이터만 출력 저장¶
In [4]:
df_GDP=df.loc[:,['시점','미국','대한민국','일본','중국','러시아','영국','인도']]
df_GDP.head()
Out[4]:
시점 | 미국 | 대한민국 | 일본 | 중국 | 러시아 | 영국 | 인도 | |
---|---|---|---|---|---|---|---|---|
0 | 1995 | 2.7 | 9.6 | 2.6 | 11.0 | -4.1 | 2.5 | 7.6 |
1 | 1996 | 3.8 | 7.9 | 3.1 | 9.9 | -3.8 | 2.4 | 7.5 |
2 | 1997 | 4.4 | 6.2 | 1.0 | 9.2 | 1.4 | 4.9 | 4.0 |
3 | 1998 | 4.5 | -5.1 | -1.3 | 7.8 | -5.3 | 3.2 | 6.2 |
4 | 1999 | 4.8 | 11.5 | -0.3 | 7.7 | 6.4 | 3.0 | 8.8 |
주요국가 시각화¶
In [ ]:
plt.figure(figsize = (10, 5), dpi = 300)#
plt.rc('font', family='Malgun Gothic')
plt.title('주요 국가 GDP 변화')
plt.plot(df_GDP['시점'], df_GDP['미국'], label = '미국')
plt.plot(df_GDP['시점'], df_GDP['대한민국'], label = '대한민국 ',color='red', marker='o', linestyle='dashed', linewidth=2, markersize=5)
plt.plot(df_GDP['시점'], df_GDP['일본'], label = '일본')
plt.plot(df_GDP['시점'], df_GDP['중국'], label = '중국')
plt.plot(df_GDP['시점'], df_GDP['러시아'], label = '러시아')
plt.plot(df_GDP['시점'], df_GDP['인도'], label = '인도')
plt.legend()
plt.xticks(rotation=50)
plt.savefig('주요국가 GDP그래프.png', facecolor='#eeeeee')
plt.show()
한중일 GDP 차이 시각화¶
In [6]:
import matplotlib
matplotlib.rcParams['axes.unicode_minus'] = False
plt.figure(figsize = (10, 5), dpi = 300)#
plt.rc('font', family='Malgun Gothic')
plt.title('한중일 국가 GDP 변화')
plt.plot(df_GDP['시점'], df_GDP['대한민국'], label = '대한민국 ',color='red', marker='o', linestyle='dashed', markersize=5)
plt.plot(df_GDP['시점'], df_GDP['일본'], label = '일본')
plt.plot(df_GDP['시점'], df_GDP['중국'], label = '중국')
plt.legend()
plt.xticks(rotation=50)
plt.savefig('한중일 GDP그래프.png', facecolor='#eeeeee')
plt.show()
한미 GDP그래프¶
In [7]:
plt.figure(figsize = (10, 5), dpi = 300)#
plt.rc('font', family='Malgun Gothic')
plt.title('한미 국가 GDP 변화')
plt.plot(df_GDP['시점'], df_GDP['대한민국'], label = '대한민국 ',color='red', marker='o', linestyle='dashed', markersize=5)
plt.plot(df_GDP['시점'], df_GDP['미국'], label = '미국')
plt.legend()
plt.xticks(rotation=50)
plt.savefig('한미 GDP그래프.png', facecolor='#eeeeee')
plt.show()
In [12]:
df_GDP2020=df_GDP.iloc[1:-1]
df_GDP2020=df_GDP2020.reset_index(drop=True)
df_GDP2020
Out[12]:
시점 | 미국 | 대한민국 | 일본 | 중국 | 러시아 | 영국 | 인도 | |
---|---|---|---|---|---|---|---|---|
0 | 1996 | 3.8 | 7.9 | 3.1 | 9.9 | -3.8 | 2.4 | 7.5 |
1 | 1997 | 4.4 | 6.2 | 1.0 | 9.2 | 1.4 | 4.9 | 4.0 |
2 | 1998 | 4.5 | -5.1 | -1.3 | 7.8 | -5.3 | 3.2 | 6.2 |
3 | 1999 | 4.8 | 11.5 | -0.3 | 7.7 | 6.4 | 3.0 | 8.8 |
4 | 2000 | 4.1 | 9.1 | 2.8 | 8.5 | 10.0 | 3.7 | 3.8 |
5 | 2001 | 1.0 | 4.9 | 0.4 | 8.3 | 5.1 | 2.1 | 4.8 |
6 | 2002 | 1.7 | 7.7 | 0.0 | 9.1 | 4.7 | 2.1 | 3.8 |
7 | 2003 | 2.8 | 3.1 | 1.5 | 10.0 | 7.3 | 3.0 | 7.9 |
8 | 2004 | 3.9 | 5.2 | 2.2 | 10.1 | 7.2 | 2.4 | 7.9 |
9 | 2005 | 3.5 | 4.3 | 1.8 | 11.4 | 6.4 | 2.6 | 7.9 |
10 | 2006 | 2.8 | 5.3 | 1.4 | 12.7 | 8.2 | 2.6 | 8.1 |
11 | 2007 | 2.0 | 5.8 | 1.5 | 14.2 | 8.5 | 2.3 | 7.7 |
12 | 2008 | 0.1 | 3.0 | -1.2 | 9.7 | 5.2 | -0.2 | 3.1 |
13 | 2009 | -2.6 | 0.8 | -5.7 | 9.4 | -7.8 | -4.2 | 7.9 |
14 | 2010 | 2.7 | 6.8 | 4.1 | 10.6 | 4.5 | 2.1 | 8.5 |
15 | 2011 | 1.5 | 3.7 | 0.0 | 9.6 | 4.3 | 1.5 | 5.2 |
16 | 2012 | 2.3 | 2.4 | 1.4 | 7.9 | 4.0 | 1.5 | 5.5 |
17 | 2013 | 1.8 | 3.2 | 2.0 | 7.8 | 1.8 | 1.9 | 6.4 |
18 | 2014 | 2.3 | 3.2 | 0.3 | 7.4 | 0.7 | 3.0 | 7.4 |
19 | 2015 | 2.7 | 2.8 | 1.6 | 7.0 | -2.0 | 2.6 | 8.0 |
20 | 2016 | 1.7 | 2.9 | 0.8 | 6.8 | 0.2 | 2.3 | 8.3 |
21 | 2017 | 2.3 | 3.2 | 1.7 | 6.9 | 1.8 | 2.1 | 6.8 |
22 | 2018 | 2.9 | 2.9 | 0.6 | 6.7 | 2.8 | 1.7 | 6.5 |
23 | 2019 | 2.3 | 2.2 | -0.2 | 6.0 | 2.2 | 1.7 | 3.7 |
24 | 2020 | -3.4 | -0.7 | -4.5 | 2.2 | -2.7 | -9.3 | -6.6 |
대한민국 소비자물가지수 상승률로 변환하기¶
In [ ]:
#년도별 한중일 소비자 물가지수 상승률
In [24]:
#년도별 한중일 소비자 물가지수 상승률
import csv
f = open('sss.csv')
data = csv.reader(f)
max_date = []
kor =[]
kor_price_growth=[]
name1 = '대한민국'
name4 = '국가별'
for row in data:
if name1 in row[0]:
for i in row[1:27:]:
kor.append(float(i))
elif name4 in row[0]:
for i in row[2:27:]:
max_date.append(int(i))
for i in range(0,25):
kor_price_growth.append(kor[i+1]/kor[i])
In [25]:
sobi=pd.DataFrame(kor_price_growth)
sobi
Out[25]:
0 | |
---|---|
0 | 1.049270 |
1 | 1.045217 |
2 | 1.074875 |
3 | 1.007740 |
4 | 1.023041 |
5 | 1.040541 |
6 | 1.027417 |
7 | 1.035112 |
8 | 1.035278 |
9 | 1.027523 |
10 | 1.022959 |
11 | 1.024938 |
12 | 1.047445 |
13 | 1.027875 |
14 | 1.029379 |
15 | 1.039517 |
16 | 1.022175 |
17 | 1.012397 |
18 | 1.013265 |
19 | 1.007049 |
20 | 1.010000 |
21 | 1.018812 |
22 | 1.015549 |
23 | 1.003828 |
24 | 1.004766 |
In [26]:
sobi.columns = ['상승률']
sobi
Out[26]:
상승률 | |
---|---|
0 | 1.049270 |
1 | 1.045217 |
2 | 1.074875 |
3 | 1.007740 |
4 | 1.023041 |
5 | 1.040541 |
6 | 1.027417 |
7 | 1.035112 |
8 | 1.035278 |
9 | 1.027523 |
10 | 1.022959 |
11 | 1.024938 |
12 | 1.047445 |
13 | 1.027875 |
14 | 1.029379 |
15 | 1.039517 |
16 | 1.022175 |
17 | 1.012397 |
18 | 1.013265 |
19 | 1.007049 |
20 | 1.010000 |
21 | 1.018812 |
22 | 1.015549 |
23 | 1.003828 |
24 | 1.004766 |
In [27]:
result3 = pd.concat([df_GDP2020,sobi],axis=1)
result3
Out[27]:
시점 | 미국 | 대한민국 | 일본 | 중국 | 러시아 | 영국 | 인도 | 상승률 | |
---|---|---|---|---|---|---|---|---|---|
0 | 1996 | 3.8 | 7.9 | 3.1 | 9.9 | -3.8 | 2.4 | 7.5 | 1.049270 |
1 | 1997 | 4.4 | 6.2 | 1.0 | 9.2 | 1.4 | 4.9 | 4.0 | 1.045217 |
2 | 1998 | 4.5 | -5.1 | -1.3 | 7.8 | -5.3 | 3.2 | 6.2 | 1.074875 |
3 | 1999 | 4.8 | 11.5 | -0.3 | 7.7 | 6.4 | 3.0 | 8.8 | 1.007740 |
4 | 2000 | 4.1 | 9.1 | 2.8 | 8.5 | 10.0 | 3.7 | 3.8 | 1.023041 |
5 | 2001 | 1.0 | 4.9 | 0.4 | 8.3 | 5.1 | 2.1 | 4.8 | 1.040541 |
6 | 2002 | 1.7 | 7.7 | 0.0 | 9.1 | 4.7 | 2.1 | 3.8 | 1.027417 |
7 | 2003 | 2.8 | 3.1 | 1.5 | 10.0 | 7.3 | 3.0 | 7.9 | 1.035112 |
8 | 2004 | 3.9 | 5.2 | 2.2 | 10.1 | 7.2 | 2.4 | 7.9 | 1.035278 |
9 | 2005 | 3.5 | 4.3 | 1.8 | 11.4 | 6.4 | 2.6 | 7.9 | 1.027523 |
10 | 2006 | 2.8 | 5.3 | 1.4 | 12.7 | 8.2 | 2.6 | 8.1 | 1.022959 |
11 | 2007 | 2.0 | 5.8 | 1.5 | 14.2 | 8.5 | 2.3 | 7.7 | 1.024938 |
12 | 2008 | 0.1 | 3.0 | -1.2 | 9.7 | 5.2 | -0.2 | 3.1 | 1.047445 |
13 | 2009 | -2.6 | 0.8 | -5.7 | 9.4 | -7.8 | -4.2 | 7.9 | 1.027875 |
14 | 2010 | 2.7 | 6.8 | 4.1 | 10.6 | 4.5 | 2.1 | 8.5 | 1.029379 |
15 | 2011 | 1.5 | 3.7 | 0.0 | 9.6 | 4.3 | 1.5 | 5.2 | 1.039517 |
16 | 2012 | 2.3 | 2.4 | 1.4 | 7.9 | 4.0 | 1.5 | 5.5 | 1.022175 |
17 | 2013 | 1.8 | 3.2 | 2.0 | 7.8 | 1.8 | 1.9 | 6.4 | 1.012397 |
18 | 2014 | 2.3 | 3.2 | 0.3 | 7.4 | 0.7 | 3.0 | 7.4 | 1.013265 |
19 | 2015 | 2.7 | 2.8 | 1.6 | 7.0 | -2.0 | 2.6 | 8.0 | 1.007049 |
20 | 2016 | 1.7 | 2.9 | 0.8 | 6.8 | 0.2 | 2.3 | 8.3 | 1.010000 |
21 | 2017 | 2.3 | 3.2 | 1.7 | 6.9 | 1.8 | 2.1 | 6.8 | 1.018812 |
22 | 2018 | 2.9 | 2.9 | 0.6 | 6.7 | 2.8 | 1.7 | 6.5 | 1.015549 |
23 | 2019 | 2.3 | 2.2 | -0.2 | 6.0 | 2.2 | 1.7 | 3.7 | 1.003828 |
24 | 2020 | -3.4 | -0.7 | -4.5 | 2.2 | -2.7 | -9.3 | -6.6 | 1.004766 |
한국 GDP와 물가상승률 비교¶
In [19]:
plt.figure(figsize = (10, 5), dpi = 300)#
plt.rc('font', family='Malgun Gothic')
plt.title('한국 GDP 와 물가상승률 비교')
plt.subplot(2,1,1)
plt.plot(result3['시점'], result3['대한민국'], label = 'GDP ')
plt.xlabel('한국 GDP ')
plt.legend()
plt.subplots_adjust(left=0.125, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.35)
plt.subplot(2,1,2)
plt.plot(result3['시점'], result3['상승률'], label = '소비자물가')
plt.xlabel('소비자물가 상승율')
plt.legend()
plt.savefig('한미 GDP 상승률.png', facecolor='#eeeeee')
plt.show()
In [ ]:
'파이썬 활용 > 그래프 시각화' 카테고리의 다른 글
특정지역 성별 인구통계를 통한 점 그래프 활용 (0) | 2022.10.25 |
---|---|
아주 유용 1.지역별 인구 통계 분석 2. 서울 지하철 노선별 시간별 사용 (0) | 2022.10.25 |
기온 데이터 분석_문자열 함수로 전처리 후 시각화 (0) | 2022.10.25 |
seaborn을 활용한 시각화_펭귄종류에 대한 신체정보 데이터셋 (0) | 2022.10.25 |
타이타익 데이터셋을 통한 상관분석 시각화하기(다양한 그래프 활용) (0) | 2022.10.25 |