-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid_2.py
107 lines (81 loc) · 3.59 KB
/
covid_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import requests
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import collections
csv_url = 'https://covid.ourworldindata.org/data/owid-covid-data.csv'
req = requests.get(csv_url, allow_redirects = True)
open('owid-covid-data.csv', 'wb').write(req.content)
# Settings:
pd.set_option('display.max_rows', 1000)
# Global vars:
useful_columns = ['date',
'location',
'new_cases',
'total_cases',
'total_deaths']
today = datetime.date(datetime.now())
yesterday = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
covid_df = pd.read_csv('owid-covid-data.csv')
country_list = covid_df['location'].drop_duplicates().to_list()
alphabet = collections.defaultdict(list)
for word in country_list:
alphabet[word[0].upper()].append(word)
for key in alphabet:
print('{}:'.format(key))
print('\n'.join(alphabet[key]))
print()
def check(country):
return country.title() in country_list
def search(country):
# Returns a data frame of the country
print(country.title())
covid_list = covid_df[covid_df['location'] == country.title()]
covid_list = covid_list[useful_columns]
# covid_list.set_index('date', inplace = True)
df_graph = pd.DataFrame({'New cases': covid_list['new_cases'].to_list(),
'Total Deaths': covid_list['total_deaths'].to_list(),
'Total Cases': covid_list['total_cases'].to_list()}, index = covid_list['date'])
df_graph.index = pd.to_datetime(df_graph.index)
print(df_graph)
df_graph = df_graph.ffill()
return df_graph
def ranking():
ranking = covid_df[covid_df['date'] == yesterday]
ranking = ranking[ranking['location'] != 'World']
ranking = ranking[['location',
'new_cases',
'total_cases',
'total_deaths']]
ranking = ranking.sort_values(['total_deaths', 'total_cases'], ascending = False)
ranking.set_index('location', inplace = True)
# print(ranking.round().astype(int))
ranking_list = (ranking.index[:3].tolist(), ranking[:3]['total_cases'])
print('Covid-19 total cases ranking (' + str(today) + '): ')
print('1st: {country} ({qtd:2d} cases)'.format(country = ranking_list[0][0],
qtd = int(ranking_list[1][0])))
print('2nd: {country} ({qtd:2d} cases)'.format(country = ranking_list[0][1],
qtd = int(ranking_list[1][1])))
print('3rd: {country} ({qtd:2d} cases)'.format(country = ranking_list[0][2],
qtd = int(ranking_list[1][2])))
ranking()
country = input('Country to search: ')
while True:
if check(country):
# If the country exists at our list...
print('Found')
graph = search(country)
graph.plot()
# plt.gcf().autofmt_xdate(bottom=0.2, rotation=45, ha='right', which=None)
plt.show()
if str(input("""Do you wan't to search again? (Y/N)\n""")).lower() == 'y':
country = input('Country name: ')
else:
break
else:
print('Country not found')
if str(input("""Do you wan't to search again? (Y/N)\n""")).lower() == 'y':
country = input('Country name: ')
else:
break