-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_hitting_data.py
More file actions
55 lines (46 loc) · 1.35 KB
/
get_hitting_data.py
File metadata and controls
55 lines (46 loc) · 1.35 KB
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
"""
MLB Hitting Data
Author: Patrick Mejia
Date: 6-13-2025
"""
import requests
import polars as pl
def get_hitting_data(year: int) -> pl.DataFrame:
try:
url = (
f"https://statsapi.mlb.com/api/v1/stats"
f"?stats=season&group=hitting&gameType=R&season={year}"
)
print(url)
response = requests.get(url)
response.raise_for_status()
json_data = response.json()
records = []
for player_stat in json_data["stats"][0]["splits"]:
player = player_stat["player"]
stat = player_stat["stat"]
record = {
"Name": f"{player['firstName']} {player['lastName']}",
"Year": year,
**stat
}
records.append(record)
except Exception as e:
print(f"{e}")
df = pl.DataFrame(records)
print(df)
df.write_csv(f"Stats/hitting/mlb_hitting_data_{year}.csv")
return df
def main():
total_df = []
for year in range(2020, 2025):
print(f"{year} Hitting Stats")
df = get_hitting_data(year)
if df.height > 0:
total_df.append(df)
if total_df:
combined_df = pl.concat(total_df, how="vertical")
combined_df.write_csv("Stats/hitting/full_mlb_hitting_data.csv")
print(combined_df)
if __name__ == "__main__":
main()