This file contains 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 numpy as np | |
import requests | |
#Request API | |
url = 'https://corona.lmao.ninja/historical' | |
r = requests.get(url) | |
j = r.json() | |
#Create dataframe |
This file contains 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
#Convert values | |
df['date'] = pd.to_datetime(df['date']) | |
df['total'] = pd.to_numeric(df['total']) | |
#Summary table | |
df = pd.pivot_table(df, index = ['country','date'], columns = ['types'], aggfunc = np.sum) | |
df.columns = df.columns.droplevel(0) | |
df = df.reset_index() |
This file contains 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
Sub to_csv() | |
Dim InputCsvFile As Variant | |
Dim InputFolder As String | |
Dim OutputFolder As String | |
Dim ChooseFolder As FileDialog | |
Application.DisplayAlerts = False | |
Application.ScreenUpdating = False | |
Application.EnableEvents = False | |
Application.Calculation = xlCalculationManual |
This file contains 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
nor = df.groupby('country').transform(lambda x: (x - x.min()) / (x.max() - x.min())) | |
nor = nor['keep1','keep2','keep3'] | |
nor = nor.add_prefix('nor_') |
This file contains 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
std = df.groupby('country').transform(lambda x: (x - x.mean()) / x.std()) | |
std = std['keep1','keep2','keep3'] | |
std = std.add_prefix('std_') |
This file contains 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
data want; | |
set have; | |
if test_1 < 50 then flag = 1; | |
else flag = 0; | |
run; |
This file contains 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
def checking(df): | |
if df['test'] < 50: | |
flag = 1 | |
else: | |
flag = 0 | |
return flag | |
df['apply_1'] = df.apply(checking, axis = 1) |
This file contains 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 numpy as np | |
import time | |
#Mock data | |
data = np.random.randint(0,100,size = (2**20, 1)) | |
df = pd.DataFrame(data, columns = ['test']) | |
#Pandas apply | |
s = time.time() |
This file contains 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
#Numpy where and select | |
s = time.time() | |
df['where'] = np.where(df['test'] < 50, 1, 0) | |
e = time.time() | |
print('where time = {}'.format(e-s)) | |
s = time.time() | |
df['select'] = np.select([df['test'] < 30, df['test'] < 50], [1,2], default = 3) | |
e = time.time() | |
print('Select time = {}'.format(e-s)) |
This file contains 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
#Calculation | |
#Marginal cases | |
l = list(df.columns) | |
l = l[2:] | |
for i in l: | |
df['daily_'+str(i)] = df.groupby(['country'])[str(i)].diff(1).fillna(df[str(i)]) | |
#Decrease cumulative | |
df['current'] = df['cases'] - df['recovered'] - df['deaths'] | |
l.append('current') |
OlderNewer