Commit
·
24ffe1d
1
Parent(s):
12c5ad7
Update app.py
Browse files
app.py
CHANGED
@@ -92,14 +92,18 @@ class Stats:
|
|
92 |
|
93 |
# Get unique names
|
94 |
names = {}
|
95 |
-
rows = self.stats[["nameFirst", "nameLast", "playerID"]]
|
96 |
for _, row in rows.iterrows():
|
97 |
# Name key
|
98 |
key = f"{row['nameFirst']} {row['nameLast']}"
|
99 |
-
|
100 |
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
103 |
|
104 |
return names
|
105 |
|
@@ -138,7 +142,7 @@ class Stats:
|
|
138 |
|
139 |
if name in self.names:
|
140 |
# Get player stats
|
141 |
-
stats = self.stats[self.stats["playerID"] == self.names[name]]
|
142 |
|
143 |
# Build key metrics
|
144 |
metrics = stats[["yearID", self.metric()]]
|
@@ -169,7 +173,8 @@ class Stats:
|
|
169 |
query = self.vector(row)
|
170 |
else:
|
171 |
# Lookup player key and build vector id
|
172 |
-
|
|
|
173 |
query = self.vectors.get(query)
|
174 |
|
175 |
results, ids = [], set()
|
@@ -254,7 +259,7 @@ class Batting(Stats):
|
|
254 |
batting = pd.merge(players, batting, how="inner", on=["playerID"])
|
255 |
|
256 |
# Require player to have at least 350 plate appearances.
|
257 |
-
batting = batting[(batting["AB"] + batting["BB"]) >= 350]
|
258 |
|
259 |
# Derive primary player positions
|
260 |
positions = self.positions(fielding)
|
@@ -387,7 +392,7 @@ class Pitching(Stats):
|
|
387 |
pitching = pd.merge(players, pitching, how="inner", on=["playerID"])
|
388 |
|
389 |
# Require player to have 20 appearances
|
390 |
-
pitching = pitching[pitching["G"] >= 20]
|
391 |
|
392 |
# Calculated columns
|
393 |
pitching["age"] = pitching["yearID"] - pitching["birthYear"]
|
@@ -435,6 +440,8 @@ class Application:
|
|
435 |
"""
|
436 |
)
|
437 |
|
|
|
|
|
438 |
self.player()
|
439 |
|
440 |
def player(self):
|
@@ -452,8 +459,7 @@ class Application:
|
|
452 |
stats = self.batting if category == "Batting" else self.pitching
|
453 |
|
454 |
# Player name
|
455 |
-
|
456 |
-
name = self.name(names, params.get("name"))
|
457 |
|
458 |
# Player metrics
|
459 |
active, best, metrics = stats.metrics(name)
|
@@ -470,7 +476,7 @@ class Application:
|
|
470 |
|
471 |
# Display results
|
472 |
self.table(results, ["nameFirst", "nameLast", "teamID"] + stats.columns[1:] + ["link"])
|
473 |
-
|
474 |
# Save parameters
|
475 |
st.experimental_set_query_params(category=category, name=name, year=year)
|
476 |
|
@@ -528,8 +534,11 @@ class Application:
|
|
528 |
name component
|
529 |
"""
|
530 |
|
531 |
-
# Get name parameter, default to random value if not valid
|
532 |
-
name = name if name and name in names else random.
|
|
|
|
|
|
|
533 |
|
534 |
# Select box component
|
535 |
return st.selectbox("Name", names, names.index(name), key="name")
|
|
|
92 |
|
93 |
# Get unique names
|
94 |
names = {}
|
95 |
+
rows = self.stats[["nameFirst", "nameLast", "playerID"]]
|
96 |
for _, row in rows.iterrows():
|
97 |
# Name key
|
98 |
key = f"{row['nameFirst']} {row['nameLast']}"
|
99 |
+
key += f" ({row['playerID']})" if key in names and names[key][0] != row["playerID"] else ""
|
100 |
|
101 |
+
if key not in names:
|
102 |
+
# Get count of active seasons for player
|
103 |
+
count = len(rows[rows["playerID"] == row["playerID"]])
|
104 |
+
|
105 |
+
# Save name key - values pair
|
106 |
+
names[key] = (row["playerID"], count)
|
107 |
|
108 |
return names
|
109 |
|
|
|
142 |
|
143 |
if name in self.names:
|
144 |
# Get player stats
|
145 |
+
stats = self.stats[self.stats["playerID"] == self.names[name][0]]
|
146 |
|
147 |
# Build key metrics
|
148 |
metrics = stats[["yearID", self.metric()]]
|
|
|
173 |
query = self.vector(row)
|
174 |
else:
|
175 |
# Lookup player key and build vector id
|
176 |
+
name = self.names.get(name)
|
177 |
+
query = f"{year}{name[0] if name else name}"
|
178 |
query = self.vectors.get(query)
|
179 |
|
180 |
results, ids = [], set()
|
|
|
259 |
batting = pd.merge(players, batting, how="inner", on=["playerID"])
|
260 |
|
261 |
# Require player to have at least 350 plate appearances.
|
262 |
+
batting = batting[((batting["AB"] + batting["BB"]) >= 350) & (batting["stint"] == 1)]
|
263 |
|
264 |
# Derive primary player positions
|
265 |
positions = self.positions(fielding)
|
|
|
392 |
pitching = pd.merge(players, pitching, how="inner", on=["playerID"])
|
393 |
|
394 |
# Require player to have 20 appearances
|
395 |
+
pitching = pitching[(pitching["G"] >= 20) & (pitching["stint"] == 1)]
|
396 |
|
397 |
# Calculated columns
|
398 |
pitching["age"] = pitching["yearID"] - pitching["birthYear"]
|
|
|
440 |
"""
|
441 |
)
|
442 |
|
443 |
+
player, search = st.tabs(["Player", "Search"])
|
444 |
+
|
445 |
self.player()
|
446 |
|
447 |
def player(self):
|
|
|
459 |
stats = self.batting if category == "Batting" else self.pitching
|
460 |
|
461 |
# Player name
|
462 |
+
name = self.name(stats.names, params.get("name"))
|
|
|
463 |
|
464 |
# Player metrics
|
465 |
active, best, metrics = stats.metrics(name)
|
|
|
476 |
|
477 |
# Display results
|
478 |
self.table(results, ["nameFirst", "nameLast", "teamID"] + stats.columns[1:] + ["link"])
|
479 |
+
|
480 |
# Save parameters
|
481 |
st.experimental_set_query_params(category=category, name=name, year=year)
|
482 |
|
|
|
534 |
name component
|
535 |
"""
|
536 |
|
537 |
+
# Get name parameter, default to random weighted value if not valid
|
538 |
+
name = name if name and name in names else random.choices(list(names.keys()), weights=[names[x][1] for x in names])[0]
|
539 |
+
|
540 |
+
# Sort names for display
|
541 |
+
names = sorted(names)
|
542 |
|
543 |
# Select box component
|
544 |
return st.selectbox("Name", names, names.index(name), key="name")
|