-
Notifications
You must be signed in to change notification settings - Fork 0
/
covid_business.py
129 lines (95 loc) · 4.36 KB
/
covid_business.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from os import defpath
import pandas as pd
class CovidBusiness():
def __init__(self, dataset_file):
df = pd.read_csv(dataset_file)
df = df.rename(columns = {'Country/Region':'Country',
'New cases': 'NewCases',
'New deaths': 'NewDeaths',
'New recovered': 'NewRecovered',
'WHO Region':'Region'})
self.dataset = df
'''
Method - Filter by Confirmed Cases
'''
def get_countries(self):
countries = self.dataset['Country']
countries = countries.drop_duplicates()
collection = {'countries' : [key for key in countries]}
return collection
def filter_by_confirmed(self, country=None, start=None, end=None):
if country == None or country == "":
filter = self.dataset.query(f'Date >= "{start}" & Date <= "{end}" ')
else:
filter = self.dataset.query(f'Country == "{country}" & \
Date >= "{start}" & Date <= "{end}"')
#agregation
group_by = dict(filter.groupby(['Country']).sum()["Confirmed"])
#transform
collection = [{"country" : key, "value" : int(group_by[key]) } for key in group_by.keys()]
collection = sorted(collection, key = lambda i: i['value'],reverse=True)
return collection
'''
Method - Filter by Death Cases
'''
def filter_by_death(self, country=None, start=None, end=None):
if country == None or country == "":
filter = self.dataset.query(f'Date >= "{start}" & Date <= "{end}" ')
else:
filter = self.dataset.query(f'Country == "{country}" & \
Date >= "{start}" & Date <= "{end}"')
#agregation
group_by = dict(filter.groupby(['Country']).sum()["Deaths"])
#transform
collection = [{"country" : key, "value" : int(group_by[key]) } for key in group_by.keys()]
collection = sorted(collection, key = lambda i: i['value'],reverse=True)
return collection
'''
Method - Filter by Recovered Cases
'''
def filter_by_recovered(self, country=None, start=None, end=None):
if country == None or country == "":
filter = self.dataset.query(f'Date >= "{start}" & Date <= "{end}" ')
else:
filter = self.dataset.query(f'Country == "{country}" & \
Date >= "{start}" & Date <= "{end}"')
#agregation
group_by = dict(filter.groupby(['Country']).sum()["Recovered"])
#transform
collection = [{"country" : key, "value" : int(group_by[key]) } for key in group_by.keys()]
collection = sorted(collection, key = lambda i: i['value'],reverse=True)
return collection
'''
Method - Filter by Active Cases
'''
def filter_by_active(self, country=None, start=None, end=None):
if country == None or country == "":
filter = self.dataset.query(f'Date >= "{start}" & Date <= "{end}" ')
else:
filter = self.dataset.query(f'Country == "{country}" & \
Date >= "{start}" & Date <= "{end}"')
#agregation
group_by = dict(filter.groupby(['Country']).sum()["Active"])
#transform
collection = [{"country" : key, "value" : int(group_by[key]) } for key in group_by.keys()]
collection = sorted(collection, key = lambda i: i['value'],reverse=True)
return collection
'''
Method - Moving Average
'''
def moving_average(self, country=None, start=None, end=None, window=int):
if country == None or country == "":
return { "error" : "Parâmetro country necessário."}
else:
filter = self.dataset.query(f'Country == "{country}" & \
Date >= "{start}" & Date <= "{end}"')
filter = filter.set_index('Date')
confirmed = filter["Confirmed"].rolling(window = window).mean().fillna(0)
deaths = filter["Deaths"].rolling(window = window).mean().fillna(0)
active = filter["Active"].rolling(window = window).mean().fillna(0)
recovered = filter["Recovered"].rolling(window = window).mean().fillna(0)
return {
"confirmed" : [ {"index" : key, "ma" : confirmed[key] } for key in confirmed.keys() ],
"deaths" : [ {"index" : key, "ma" : deaths[key] } for key in deaths.keys() ],
"active" : [ {"index" : key, "ma" : active[key] } for key in active.keys() ],
"recovered" : [ {"index" : key, "ma" : recovered[key] } for key in recovered.keys() ], }