[Python] Covid-19
#pandas의 import 컨벤션
import pandas as pd
#numpy의 import 컨벤션
import numpy as np
[문1]
일별 국가별 코로나 발생자수와 사망자 수를 기준으로 전처리 하시오.
일부가는 지역별로 코로나 발생자수와 사망자 수가 분리되어 있으니 국가별로 집계하고
국가, 총발생자수, 총사망자수, 일평균 발생자수, 일평균 사망자수 리스트를 제시하시오.
# 1단계 : 2021년 2020년 파일 불러오기
data2021 = pd.read_csv('C:/12-31-2020.csv')
data2020 = pd.read_csv('C:/12-31-2020.csv')
# 2단계 : 2021년 2020년 에서 데이터에서 나라가 아닌 'Olympics' 제거하기
# 2-1단계 : data2021,2020 의 index인 'Country_Region'에서 'Olympics'를 포함한 단어 찾기
del_olympics = data2021[data2021['Country_Region'].str.contains('Olympics')].index
del_olympics2 = data2020[data2020['Country_Region'].str.contains('Olympics')].index
# 2-2단계 : data2021,2020 에서 'Olympics'를 포함한 행을 삭제하기
data2021.drop(del_olympics, inplace=True)
data2020.drop(del_olympics2, inplace=True)
# 3단계 : data2021,2020 에서 필요한 column 선택하기
subset2021 = data2021 [['Country_Region','Confirmed','Deaths']]
subset2020 = data2020 [['Country_Region','Confirmed','Deaths']]
# 4단계 : subset2020,2021 에서 나라별 중복제거해서 합치기
group1=subset2020.groupby('Country_Region').sum()
group2=subset2021.groupby('Country_Region').sum()
# 5
# 5-1단계 : 2021년 총 발생자 & 사망자수 구하기
tot_condea = group2.sub(group1)
# 5-2단계 : 2021년 일평균 발생자 & 사망자수 구하기
daily_condea = tot_condea / 365
# 6단계 : 컬럼명 입력하기
# 6-1단계 : 총 발생자 & 사망자의 column명 설정하기
tot = tot_condea.rename(columns = {'Confirmed' : 'tot_con', 'Deaths' : 'tot_dea'})
# 6-2단계 : 일평균 발생자 & 사망자의 column명 설정하기
daily = daily_condea.rename(columns = {'Confirmed' : 'daily_con', 'Deaths' : 'daily_dea'})
# 7단계 : 총 발생자 & 사망자와 일평균 발생자 & 사망자를 열기준으로 합치기
result = pd.concat([tot,daily],axis=1)
# 8단계 : 인덱스 재설정하기
final = result.reset_index()
# 9단계 : column명 가운데 정렬하기
pd.set_option('display.colheader_justify', 'center')
# 10단계 : 데이터 출력
print(final)
# <출력>
# Country_Region tot_con tot_dea daily_con daily_dea
# 0 Afghanistan 105754.0 5167.0 289.736986 14.156164
# 1 Albania 151908.0 2036.0 416.186301 5.578082
# 2 Algeria 118822.0 3520.0 325.539726 9.643836
# 3 Andorra 15691.0 56.0 42.989041 0.153425
# 4 Angola 64040.0 1365.0 175.452055 3.739726
# .. ... ... ... ... ...
# 192 Vietnam 1729792.0 32359.0 4739.156164 88.654795
# 193 West Bank and Gaza 331744.0 3519.0 908.887671 9.641096
# 194 Yemen 8027.0 1374.0 21.991781 3.764384
# 195 Zambia 233549.0 3346.0 639.860274 9.167123
# 196 Zimbabwe 199391.0 4641.0 546.276712 12.715068
# [197 rows x 5 columns]
[문2]
데이터가 0인 경우(코로나 환자 0)와 데이터가 없는 경우를 구분하여 전처리하고
전처리 시 data가 없는 국가는 제외하고 제외된 국가 리스트를 제시하오.
# 값이 0인 데이터 찾기
Ans1 = final.loc[final['tot_con']==0]
# 값이 NaN인 데이터 찾기
Ans2 = final[final['tot_con'].isnull()]
# 값이 0인 데이터 국가명만 뽑아내기
print(Ans1['Country_Region'])
# <출력>
# 49 Diamond Princess
# 76 Holy See
# 93 Korea, North
# 107 MS Zaandam
# 114 Marshall Islands
# Name: Country_Region, dtype: object
# 값이 NaN인 국가명만 뽑아내기
print(Ans2['Country_Region'])
# <출력>
# 118 Micronesia
# Name: Country_Region, dtype: object
# 값이 NaN인 인덱스 빼기
final2 = final.drop(Ans2.index[0:len(Ans2)])
# 최종 결과값 국가명만 뽑아내기
final2["Country_Region"]
# <출력>
# 0 Afghanistan
# 1 Albania
# 2 Algeria
# 3 Andorra
# 4 Angola
# ...
# 192 Vietnam
# 193 West Bank and Gaza
# 194 Yemen
# 195 Zambia
# 196 Zimbabwe
# Name: Country_Region, Length: 196, dtype: object
[문3]
2021년 1년동안 코로나 총 발생자수, 총 사망자수, 일평균 발생자수, 일평균사망자 수를 기준으로
가장 많은 20개 국가를 내림차순으로 정렬하고
총 발생자수, 총 사망자수, 일평균 발생자수, 일평균 사망자 수를 리포트 하시오.
(4가지 기준 각각 sorting)
# 총 발생자 수 가장 많은 국가 내림차순으로 정렬
내림차순으로 정렬한 총 발생자 수에서 상위 20개국 출력하기
tot_con = final2.sort_values('tot_con',ascending=False)
tot_con.head(20)
# <출력>
# Country_Region tot_con tot_dea daily_con daily_dea
# 183 US 34645729.0 475132.0 94919.805479 1301.731507
# 80 India 24574870.0 332492.0 67328.410959 910.936986
# 24 Brazil 14610807.0 424262.0 40029.608219 1162.361644
# 187 United Kingdom 10514666.0 82470.0 28807.304110 225.945205
# 63 France 7400127.0 59971.0 20274.320548 164.304110
# 182 Turkey 7273898.0 61480.0 19928.487671 168.438356
# 146 Russia 7193058.0 246400.0 19707.008219 675.068493
# 67 Germany 5446257.0 78320.0 14921.252055 214.575342
# 82 Iran 4969259.0 76383.0 13614.408219 209.268493
# 166 Spain 4366480.0 38568.0 11962.958904 105.665753
# 7 Argentina 4028894.0 73924.0 11038.065753 202.531507
# 86 Italy 4018517.0 63243.0 11009.635616 173.268493
# 81 Indonesia 3519522.0 121956.0 9642.526027 334.126027
# 38 Colombia 3514665.0 86729.0 9629.219178 237.613699
# 142 Poland 2813337.0 68500.0 7707.772603 187.671233
# 185 Ukraine 2760229.0 82807.0 7562.271233 226.868493
# 110 Malaysia 2645076.0 31016.0 7246.783562 84.975342
# 117 Mexico 2553629.0 173621.0 6996.243836 475.673973
# 164 South Africa 2401125.0 62676.0 6578.424658 171.715068
# 127 Netherlands 2374752.0 9867.0 6506.169863 27.032877
# 총 사망자 수 가장 많은 국가 내림차순으로 정렬
내림차순으로 정렬한 총 사망자 수에서 상위 20개국 출력하기
tot_dea = final2.sort_values('tot_dea',ascending=False)
tot_dea.head(20)
# <출력>
# Country_Region tot_con tot_dea daily_con daily_dea
# 183 US 34645729.0 475132.0 94919.805479 1301.731507
# 24 Brazil 14610807.0 424262.0 40029.608219 1162.361644
# 80 India 24574870.0 332492.0 67328.410959 910.936986
# 146 Russia 7193058.0 246400.0 19707.008219 675.068493
# 117 Mexico 2553629.0 173621.0 6996.243836 475.673973
# 81 Indonesia 3519522.0 121956.0 9642.526027 334.126027
# 140 Peru 1281694.0 109620.0 3511.490411 300.328767
# 38 Colombia 3514665.0 86729.0 9629.219178 237.613699
# 185 Ukraine 2760229.0 82807.0 7562.271233 226.868493
# 187 United Kingdom 10514666.0 82470.0 28807.304110 225.945205
# 67 Germany 5446257.0 78320.0 14921.252055 214.575342
# 82 Iran 4969259.0 76383.0 13614.408219 209.268493
# 7 Argentina 4028894.0 73924.0 11038.065753 202.531507
# 142 Poland 2813337.0 68500.0 7707.772603 187.671233
# 86 Italy 4018517.0 63243.0 11009.635616 173.268493
# 164 South Africa 2401125.0 62676.0 6578.424658 171.715068
# 182 Turkey 7273898.0 61480.0 19928.487671 168.438356
# 63 France 7400127.0 59971.0 20274.320548 164.304110
# 145 Romania 1176628.0 42985.0 3223.638356 117.767123
# 141 Philippines 2369915.0 42260.0 6492.917808 115.780822
# 일평균 발생자 수 가장 많은 국가 내림차순으로 정렬
내림차순으로 정렬한 일평균 발생자 수에서 상위 20개국 출력하기
daily_con = final2.sort_values('daily_con',ascending=False)
daily_con.head(20)
# <출력>
# Country_Region tot_con tot_dea daily_con daily_dea
# 183 US 34645729.0 475132.0 94919.805479 1301.731507
# 80 India 24574870.0 332492.0 67328.410959 910.936986
# 24 Brazil 14610807.0 424262.0 40029.608219 1162.361644
# 187 United Kingdom 10514666.0 82470.0 28807.304110 225.945205
# 63 France 7400127.0 59971.0 20274.320548 164.304110
# 182 Turkey 7273898.0 61480.0 19928.487671 168.438356
# 146 Russia 7193058.0 246400.0 19707.008219 675.068493
# 67 Germany 5446257.0 78320.0 14921.252055 214.575342
# 82 Iran 4969259.0 76383.0 13614.408219 209.268493
# 166 Spain 4366480.0 38568.0 11962.958904 105.665753
# 7 Argentina 4028894.0 73924.0 11038.065753 202.531507
# 86 Italy 4018517.0 63243.0 11009.635616 173.268493
# 81 Indonesia 3519522.0 121956.0 9642.526027 334.126027
# 38 Colombia 3514665.0 86729.0 9629.219178 237.613699
# 142 Poland 2813337.0 68500.0 7707.772603 187.671233
# 185 Ukraine 2760229.0 82807.0 7562.271233 226.868493
# 110 Malaysia 2645076.0 31016.0 7246.783562 84.975342
# 117 Mexico 2553629.0 173621.0 6996.243836 475.673973
# 164 South Africa 2401125.0 62676.0 6578.424658 171.715068
# 127 Netherlands 2374752.0 9867.0 6506.169863 27.032877
# 일평균 사망자 수 가장 많은 국가 내림차순으로 정렬
내림차순으로 정렬한 일평균 사망자 수에서 상위 20개국 출력하기
daily_dea = final2.sort_values('daily_dea',ascending=False)
daily_dea.head(20)
# <출력>
# Country_Region tot_con tot_dea daily_con daily_dea
# 183 US 34645729.0 475132.0 94919.805479 1301.731507
# 24 Brazil 14610807.0 424262.0 40029.608219 1162.361644
# 80 India 24574870.0 332492.0 67328.410959 910.936986
# 146 Russia 7193058.0 246400.0 19707.008219 675.068493
# 117 Mexico 2553629.0 173621.0 6996.243836 475.673973
# 81 Indonesia 3519522.0 121956.0 9642.526027 334.126027
# 140 Peru 1281694.0 109620.0 3511.490411 300.328767
# 38 Colombia 3514665.0 86729.0 9629.219178 237.613699
# 185 Ukraine 2760229.0 82807.0 7562.271233 226.868493
# 187 United Kingdom 10514666.0 82470.0 28807.304110 225.945205
# 67 Germany 5446257.0 78320.0 14921.252055 214.575342
# 82 Iran 4969259.0 76383.0 13614.408219 209.268493
# 7 Argentina 4028894.0 73924.0 11038.065753 202.531507
# 142 Poland 2813337.0 68500.0 7707.772603 187.671233
# 86 Italy 4018517.0 63243.0 11009.635616 173.268493
# 164 South Africa 2401125.0 62676.0 6578.424658 171.715068
# 182 Turkey 7273898.0 61480.0 19928.487671 168.438356
# 63 France 7400127.0 59971.0 20274.320548 164.304110
# 145 Romania 1176628.0 42985.0 3223.638356 117.767123
# 141 Philippines 2369915.0 42260.0 6492.917808 115.780822
[문4]
2021년 1년동안 대한민국에서 발생한 총 코로나 발생자수, 총 사망자 수, 일평균 발생자수, 일평균 사망자 수를 리포트 하시오. ( # 대한민국 Korea, South )
# 'Country_Region' column에서 'Korea, South'와 일치하는 데이터 추출하기
sub2=final2.loc[final2["Country_Region"]=="Korea, South"]
# 추출한 데이터 출력하기
print(sub2)
# <출력>
# Country_Region tot_con tot_dea daily_con daily_dea
# 94 Korea, South 573484.0 4708.0 1571.189041 12.89863