-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprepare_data.py
More file actions
71 lines (53 loc) · 2.19 KB
/
Copy pathprepare_data.py
File metadata and controls
71 lines (53 loc) · 2.19 KB
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
""" Grabs data from a particular source and returns a cleaned version
"""
import pandas as pd
import cleaners
import dataloaders
def australia() -> pd.DataFrame:
"""Prepare a clean dataframe of Australian data.
Args:
None
Returns:
df_aus (pd.DataFrame): The cleaned australia data
"""
# Find the correct dataframe
first_col_name = 'Unnamed: 0'
df_aus = dataloaders.load_correct_table('2020_coronavirus_pandemic_in_Australia', first_col_name)
df_aus = df_aus.rename(columns={'Unnamed: 0': 'date'})
df_aus = df_aus.set_index('date')
# Clean the data
df_aus = cleaners.remove_column_names_at_bottom(df_aus)
df_aus = cleaners.drop_reference_columns(df_aus)
df_aus = cleaners.clean_references_from_data(df_aus)
df_aus = cleaners.clean_references_from_column_names(df_aus)
df_aus = cleaners.clean_references_from_index(df_aus)
# Remove commas from data
df_aus = cleaners.remove_string_from_data(df_aus, ',')
# Drop rows that contain useless information
df_aus = df_aus[~df_aus.index.str.contains("after")]
# Drop newcases and % growth
df_aus = df_aus.drop(['Newcases', '%growth'], axis=1)
# Set everything in the dataframe to float64
df_aus = df_aus.astype('float64')
return df_aus
def australia_change(df_cleaned: pd.DataFrame) -> pd.DataFrame:
"""Return a dataframe of the daily change in cases.
Args:
df_cleaned (pd.DataFrame): A clean dataframe for Australia. Make it by using australia().
Returns:
df_change (pd.DataFrame): A dataframe containing the change in number of cases.
"""
# Make the dataframe to subtract
# Drop the last row (most recent data) of the df
df_minus_tail = df_cleaned.drop(df_cleaned.tail(1).index)
# Drop the first row of the dataframe
df_minus_head = df_cleaned.drop(df_cleaned.head(1).index)
index = df_minus_head.index
# Drop the index from both
df_minus_head = df_minus_head.reset_index(drop=True)
df_minus_tail = df_minus_tail.reset_index(drop=True)
# Subtract the two
df_change = df_minus_head.subtract(df_minus_tail)
# Put the index back in
df_change.index = index
return df_change