-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateStats.py
182 lines (147 loc) · 4.78 KB
/
generateStats.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import numpy as np
import pandas as pd
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')
import django
django.setup()
stats = pd.read_csv('data/vandymen_stats.csv')
stats = stats.drop(['Unnamed: 11', 'League Leader'], axis = 1)
teams = '''Bishop Sycamore
Captains and Civilians
Daddy Day Care
Flaming Balls, The
Gus Bus, The
Hole Patrol, The
Hojat's Heroes
Killas, The
Rooks, The
Seatte Seamen, The
Silly Salmon, The
Southside Sluggers, The
Wiffle House
Wiffle Women, The'''
teams = teams.split('\n')
abvs = 'BS CAC DAD FB GB HP HH K R SSM SAL SSL WH WW'.split()
abvs_dict = {a[0]:a[1] for a in list(zip(teams, abvs))}
df = pd.read_csv('data/vandymen_teams.csv')
df.columns = ['Division', 'NL', 'W1', 'L1', 'WP', 'nothing' , 'Division2', 'AL', 'W2', 'L2', 'WP2']
team_data = []
for index, row in df.iterrows():
if row['NL'] in teams:
team_data.append({'name': row['NL'], 'W': float(row['W1']), 'L': float(row['L1']), 'division': 'NL', 'gp': float(row['L1']) + float(row['W1'])})
print(row['NL'])
if row['AL'] in teams:
team_data.append({'name': row['AL'], 'W': float(row['W2']), 'L': float(row['L2']), 'division': 'AL', 'gp': float(row['L2']) + float(row['W2'])})
print(row['AL'])
stats = stats.rename(columns={'Unnamed: 0': 'name'})
curr_team = teams[0]
stats['team'] = stats['name'].apply(lambda x : '')
for index, row in stats.iterrows():
if row['name'] in teams:
curr_team = row['name']
else:
row['team'] = curr_team
for index, row in stats.iterrows():
if row['name'] in teams:
stats.drop(index, inplace = True)
stats = stats[stats['name'].notna()]
columns = list(stats.columns)
columns.remove('team')
columns.remove('name')
for index, row in stats.iterrows():
for c in columns:
row[c] = float(row[c])
stats = stats.fillna(0)
def calculateOBS(hits, walks, ab):
if ab == 0:
return 0
return round((hits + walks) / (ab + walks), 3)
def calculateSlug(single, double, triple, hr, ab):
if (ab == 0):
return 0
return round ((single + 2*double + 3*triple + 4*hr) / ab, 3)
stats['OBP'] = stats.apply(lambda x : calculateOBS(x['H'], x['BB'], x['AB']), axis = 1)
stats['SP'] = stats.apply(lambda x : calculateSlug(x['H'] - x['HR'] - x['3B'] - x['2B'], x['2B'], x['3B'], x['HR'], x['AB']), axis = 1)
stats['OPS'] = stats.apply(lambda x : x['OBP'] + x['SP'], axis = 1)
week1 = pd.read_csv('data/week1copy.csv')
week1 = week1[week1['Team'].notna()]
teams1 = '''Bishop Sycamore
Captains and Civilians
Daddy Day Care
Flaming Balls
Gus Bus
Hole Patrol
Hojat's Heroes
Killas
Rooks
Seatte Seamen
Silly Salmon
Southside Sluggers
Wiffle House
Wiffle Women'''
teams1 = teams1.split('\n')
teams2 = '''Bishop Sycamore
Captains and Civilians
Daddy Day Care
Flaming Balls, The
Gus Bus, The
Hole Patrol, The
Hojat's Heroes
Killas, The
Rooks, The
Seatte Seamen, The
Silly Salmon, The
Southside Sluggers, The
Wiffle House
Wiffle Women, The'''
teams2 = teams2.split('\n')
teamKeys = {x: y for x, y in zip(teams1, teams2)}
def convertInt(s):
possible_runs = [str(x) for x in range(0,20)]
if s in possible_runs:
return int(s)
else:
return None
week1['R'] = week1['R'].apply(convertInt)
def calcAvgRuns(teamName):
n = 0
runs = 0
for index, row in week1.iterrows():
if row['Team'] == teamName:
if row['R'] != None:
n += 1
runs += row['R']
if n == 0:
return 0
return runs/n
def calcAvgRunsAllowed(teamName):
n = 0
ra = 0
matchup = False
for index, row in week1.iterrows():
if '@' in row['Team']:
splitNames = row['Team'].split('@')
if splitNames[0][:len(splitNames[0])-1] == teamName or splitNames[1][1:] == teamName:
matchup = True
else:
matchup = False
if row['Team'] != teamName and row['Team'] in teams and matchup:
if row['R'] != None:
n += 1
ra += row['R']
if n == 0:
return 0
return ra/n
avgRunsAllowed = {}
avgRuns = {}
for t, y in teamKeys.items():
avgRunsAllowed[y] = calcAvgRunsAllowed(t)
avgRuns[y] = calcAvgRuns(t)
from statsApp.models import Player, Team
print('generating')
for t in team_data:
Team.objects.get_or_create(name = t['name'], wins = t['W'], losses = t['L'], division = t['division'], gp = t['gp'], abv = abvs_dict[t['name']])
for index, row in stats.iterrows():
Player.objects.get_or_create(name = row['name'], gp = row['GP'], ab = row['AB'], h = row['H'], db = row['2B'], tr = row['3B'],
hr = row['HR'], rbi = row['RBI'], k = row['K'], bb = row['BB'], ba = row['BA'], obp = row['OBP'],
sp = row['SP'], ops = row['OPS'], team = Team.objects.get(name = row['team']))