In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy import stats
%matplotlib inline
#그래프 격자로 숫자 범위 눈에 잘 띄도록 ggplot 스타일 적용
plt.style.use('ggplot')
#마이너스 폰트 문자 깨짐 해결
mpl.rcParams['axes.unicode_minus'] = False
#경고 숨기기
import warnings
warnings.filterwarnings('ignore')
#style.use('fivethirtyeight')
sns.set(style='whitegrid',color_codes=True)
In [2]:
#Load Data
data = pd.read_csv('./Corona19/Practice/WHO_report.csv')
population = pd.read_csv('./Corona19/Practice/total_population.csv')
print(data.shape, '\n', population.shape)
In [3]:
#EDA
data.isnull().sum()
data.columns
data = data[['date', 'location', 'new_cases', 'new_deaths']]
In [4]:
population.columns
population = population[['Region, subregion, country or area *', '2020']]
population.columns = ['location', 'population']
population['population'] = population['population'].str.replace(' ', '')
population['population'] = population['population'].str.replace('\.\.\.', '0')
population['population'] = population['population'].astype(int)
population.dtypes
Out[4]:
In [5]:
#누적값 구하기
data['total_cases'] = data.groupby(by = ['location'])['new_cases'].apply(lambda x : x.cumsum())
data['total_death'] = data.groupby(by = ['location'])['new_deaths'].apply(lambda x : x.cumsum())
In [6]:
#Left Join
data = pd.merge(data, population, how = 'left', on = 'location')
data = data[pd.notnull(data['population'])]
data = data.fillna(0)
data.head(10)
Out[6]:
In [7]:
#Group by location
data_gbyl = data.groupby(['location'])
data_gby_last = data_gbyl.last()
data_gby_last.sample(10)
Out[7]:
1-1. 인구대비 확진자 상위 5개국¶
In [8]:
data_gby_last['cases/population'] = data_gby_last['total_cases'] / data_gby_last['population']
data_gby_last = data_gby_last.sort_values(by = 'cases/population', ascending = False)
data_gby_last.head(5)
Out[8]:
1-2. 인구대비 사망자 상위 5개국¶
In [9]:
data_gby_last['deaths/population'] = data_gby_last['total_death'] / data_gby_last['population']
data_gby_last = data_gby_last.sort_values(by = 'deaths/population', ascending = False)
data_gby_last.head(5)
Out[9]:
1-3. 5개국 국가별 확진자 누적 그래프 그리기¶
In [10]:
top5 = data.reset_index()
top5 = top5[top5['location'].str.contains('San Marino|Iceland|Italy|Switzerland|Norway')]
pvotTop5 = top5.pivot('location', 'date', 'total_cases')
pvotTop5 = pvotTop5.fillna(0)
pvotTop5
Out[10]:
In [11]:
pvotTop5 = pvotTop5.T
pvotTop5.head()
Out[11]:
In [12]:
plt.figure(figsize=(15, 7))
plt.xticks(rotation = 45)
plt.plot(pvotTop5)
plt.show()
In [13]:
pvotTop5_Nindex = pvotTop5.reset_index()
pvotTop5_Nindex
plt.figure(figsize=(15, 7))
plt.xticks(rotation = 45)
plt.plot(pvotTop5_Nindex['date'], pvotTop5_Nindex['Iceland'])
plt.plot(pvotTop5_Nindex['date'], pvotTop5_Nindex['Norway'])
plt.plot(pvotTop5_Nindex['date'], pvotTop5_Nindex['Italy'])
plt.plot(pvotTop5_Nindex['date'], pvotTop5_Nindex['San Marino'])
plt.plot(pvotTop5_Nindex['date'], pvotTop5_Nindex['Switzerland'])
plt.show()
In [14]:
top5 = top5[top5['location'].str.contains('San Marino|Iceland|Italy|Switzerland|Norway')]
top5 = top5.sort_values(by = 'date', ascending = True)
top5
Out[14]:
In [15]:
plt.figure(figsize=(15, 7))
plt.xticks(rotation = 45)
# plt.legend(fontsize=50) # using a size in points
# plt.legend(fontsize="xx-large")
sns.lineplot(x = 'date', y = 'total_cases', data =top5, hue = 'location')
Out[15]:
In [ ]:
'Data > Data Analystics' 카테고리의 다른 글
비트코인 최대 하락률과 최대 상승률 분석하기 (1) | 2025.05.24 |
---|---|
[데이터 분석] 정말 비행기가 가장 안전한 교통 수단일까? 2. 데이터 전처리 (0) | 2024.12.30 |
[데이터 분석] 정말 비행기가 가장 안전한 교통 수단일까? 1. 데이터 수집 (0) | 2024.12.30 |
[데이터 분석] 정말 비행기가 가장 안전한 교통 수단일까? 0. 분석 계기 (1) | 2024.12.30 |
[2023 빅콘테스트] 클래식 공연 활성화를 위한 효과적 가격 모델 수립 (0) | 2024.05.01 |