Category : data-science

import pandas as pd import numpy as np import scipy.stats as stats import re nhl_df=pd.read_csv("assets/nhl.csv") cities=pd.read_html("assets/wikipedia_data.html")[1] cities=cities.iloc[:-1,[0,3,5,6,7,8]] cities.rename(columns={‘Population(2016 est.)[8]’:’Population’},inplace=True) cities[‘NFL’]=cities[‘NFL’].str.replace(r"[.*]", "") cities[‘MLB’]=cities[‘MLB’].str.replace(r"[.*]", "") cities[‘NBA’]=cities[‘NBA’].str.replace(r"[.*]", "") cities[‘NHL’]=cities[‘NHL’].str.replace(r"[.*]", "") Big4=’NHL’ def nhl_correlation(): team=cities[Big4].str.extract(‘([A-Z]{0,2}[a-z0-9]*[A-Z]{0,2}[a-z0-9]*|[A-Z]{0,2}[a-z0-9]*)([A-Z]{0,2}[a-z0-9]*[A-Z]{0,2}[a-z0-9]*|[A-Z]{0,2}[a-z0-9]*)([A-Z]{0,2}[a-z0-9]*[A-Z]{0,2}[a-z0-9]*|[A-Z]{0,2}[a-z0-9]*)’) team[‘Metropolitan area’]=cities[‘Metropolitan area’] team=pd.melt(team,id_vars=[‘Metropolitan area’]).drop(columns=[‘variable’]).replace("",np.nan).replace(‘_’,np.nan).dropna().reset_index().rename(columns={‘Value’:’team’}) team=pd.merge(team,cities,how=’left’,on=’Metropolitan area’).iloc[:,1:4] team=team.astype({‘Metropolitan area’:str,’team’:str,’Population’:int}) team[‘team’]=team[‘team’].str.replace(‘[w.]* ‘,”) _df=pd.read_csv(‘assets/’+str.lower(Big4)+’.csv’) _df=_df[_df[‘year’]==2018] _df[‘team’]=_df[‘team’].str.replace(r’*’,"") _df=_df[[‘team’,’W’,’L’]] dropList=[] for i in range(_df.shape[0]): row=_df.iloc[i] if row[‘team’]==row[‘W’] and ..

Read more

let’s say i have and dataframe as below date,ent_id,val 2021-03-23,101,61 2021-03-12,103,64 2021-03-15,101,32 2021-04-01,103,39 2021-04-02,101,71 2021-04-02,103,79 2021-04-30,101,51 2021-04-30,103,53 2021-05-31,101,28 2021-05-31,103,26 2021-05-31,101,47 2021-05-31,103,61 2021-06-06,101,45 2021-06-06,103,78 2021-06-07,101,23 2021-06-07,103,31 2021-07-31,101,14 2021-07-31,103,02 2021-07-31,101,82 2021-07-31,103,15 i want to create an addition column in dataframe which contain month end date based of following condition case when DAYNAME(‘date’)=’Sunday’ then days_add(date,-2) when DAYNAME(‘date’)=’Saturday’ then ..

Read more