Skip to content

Instantly share code, notes, and snippets.

View naenumtou's full-sized avatar
🏠
Working from home

Sasiwut Chaiyadecha naenumtou

🏠
Working from home
View GitHub Profile
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
#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()
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
nor = df.groupby('country').transform(lambda x: (x - x.min()) / (x.max() - x.min()))
nor = nor['keep1','keep2','keep3']
nor = nor.add_prefix('nor_')
std = df.groupby('country').transform(lambda x: (x - x.mean()) / x.std())
std = std['keep1','keep2','keep3']
std = std.add_prefix('std_')
data want;
set have;
if test_1 < 50 then flag = 1;
else flag = 0;
run;
def checking(df):
if df['test'] < 50:
flag = 1
else:
flag = 0
return flag
df['apply_1'] = df.apply(checking, axis = 1)
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()
#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))
#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')