Last active
November 14, 2020 01:40
-
-
Save JoeThunyathep/28d9d404aa80d59ada69278e9c8de6b9 to your computer and use it in GitHub Desktop.
COVID 19 Time Series Aggregator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import sys | |
# Example: '$ python TimeSeriesAnalysis.py 3 M' | |
number_of_countries = int(sys.argv[1]) # 3 | |
aggregation_time_interval = sys.argv[2] # 'M' | |
def covid19analysis (number_of_countries, aggregation_time_interval): | |
df = pd.read_csv(f'time_series_covid19_confirmed_global.csv') | |
df = df.drop(columns=['Province/State','Lat', 'Long']) | |
df = df.groupby('Country/Region').agg('sum') | |
dfT = df.T | |
df_time = pd.to_datetime(dfT.index) # change index to datetime | |
datetime_index = pd.DatetimeIndex(df_time.values) | |
dfT = dfT.set_index(datetime_index) | |
dfT = dfT.sort_values(by=dfT.index.values[-1], axis=1,ascending=False) | |
dfT = dfT.iloc[:,0:number_of_countries] | |
dfT = dfT.resample(aggregation_time_interval).mean() | |
output = dfT.to_json() | |
print(output) | |
sys.stdout.flush() | |
covid19analysis(number_of_countries,aggregation_time_interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment