-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManagementTeams.py
209 lines (141 loc) · 6.91 KB
/
FileManagementTeams.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File Management Repository
# last Update : 9.27.19
#### combine_Teams_to_Roster()
# Combine individual csv team files into the roster file
# Complete
#### Rosters_To_Team_Files()
# This seperates full_roster into each team
# Complete
#### Roster_lahman_tag()
# This applies lahman, retro, bbref tags to players
# Complete (some players skipped?)
import pandas as pd
#######################
#### Pull to Roster ###
#######################
# Combine individual csv team files into the roster file
def combine_Teams_to_Roster():
full_roster = []
# at the end export each team to own file in teams Folder
team_1 = pd.read_csv('data/Teams/Team1.csv', encoding='utf-8')
team_2 = pd.read_csv('data/Teams/Team2.csv', encoding='utf-8')
team_3 = pd.read_csv('data/Teams/Team3.csv', encoding='utf-8')
team_4 = pd.read_csv('data/Teams/Team4.csv', encoding='utf-8')
team_5 = pd.read_csv('data/Teams/Team5.csv', encoding='utf-8')
team_6 = pd.read_csv('data/Teams/Team6.csv', encoding='utf-8')
team_7 = pd.read_csv('data/Teams/Team7.csv', encoding='utf-8')
team_8 = pd.read_csv('data/Teams/Team8.csv', encoding='utf-8')
team_9 = pd.read_csv('data/Teams/Team9.csv', encoding='utf-8')
team_10 = pd.read_csv('data/Teams/Team10.csv', encoding='utf-8')
team_11 = pd.read_csv('data/Teams/Team11.csv', encoding='utf-8')
team_12 = pd.read_csv('data/Teams/Team12.csv', encoding='utf-8')
team_13 = pd.read_csv('data/Teams/Team13.csv', encoding='utf-8')
team_14 = pd.read_csv('data/Teams/Team14.csv', encoding='utf-8')
header = ["team_id", "Trade Block", "Positions", "Player", "Player Salary", "Keeping"]
full_roster = pd.concat([team_1, team_2, team_3, team_4, team_5,
team_6, team_7, team_8, team_9, team_10,
team_11, team_12, team_13, team_14], ignore_index=True, axis=0)
full_roster.to_csv('data/FullRoster.csv', index=False)
# Print Success when complete
print("Success")
############################
#### Roster to Team Files###
############################
def Rosters_To_Team_Files():
full_roster = pd.read_csv('data/FullRoster.csv')
# This seperates full_roster into each team
# create each team roster as an object
team_1 = full_roster[full_roster['team_id'] == 1]
team_2 = full_roster[full_roster['team_id'] == 2]
team_3 = full_roster[full_roster['team_id'] == 3]
team_4 = full_roster[full_roster['team_id'] == 4]
team_5 = full_roster[full_roster['team_id'] == 5]
team_6 = full_roster[full_roster['team_id'] == 6]
team_7 = full_roster[full_roster['team_id'] == 7]
team_8 = full_roster[full_roster['team_id'] == 8]
team_9 = full_roster[full_roster['team_id'] == 9]
team_10 = full_roster[full_roster['team_id'] == 10]
team_11 = full_roster[full_roster['team_id'] == 11]
team_12 = full_roster[full_roster['team_id'] == 12]
team_13 = full_roster[full_roster['team_id'] == 13]
team_14 = full_roster[full_roster['team_id'] == 14]
# Export Each Roster into own CSV File
header = ["team_id", "Trade Block", "Positions", "Player", "Player Salary", "Keeping", "lahmanID", "retroID", "bbrefID"]
team_1.to_csv('data/Teams/Team1.csv', encoding='utf-8', columns=header, index=False)
team_2.to_csv('data/Teams/Team2.csv', encoding='utf-8', columns=header, index=False)
team_3.to_csv('data/Teams/Team3.csv', encoding='utf-8', columns=header, index=False)
team_4.to_csv('data/Teams/Team4.csv', encoding='utf-8', columns=header, index=False)
team_5.to_csv('data/Teams/Team5.csv', encoding='utf-8', columns=header, index=False)
team_6.to_csv('data/Teams/Team6.csv', encoding='utf-8', columns=header, index=False)
team_7.to_csv('data/Teams/Team7.csv', encoding='utf-8', columns=header, index=False)
team_8.to_csv('data/Teams/Team8.csv', encoding='utf-8', columns=header, index=False)
team_9.to_csv('data/Teams/Team9.csv', encoding='utf-8', columns=header, index=False)
team_10.to_csv('data/Teams/Team10.csv', encoding='utf-8', columns=header, index=False)
team_11.to_csv('data/Teams/Team11.csv', encoding='utf-8', columns=header, index=False)
team_12.to_csv('data/Teams/Team12.csv', encoding='utf-8', columns=header, index=False)
team_13.to_csv('data/Teams/Team13.csv', encoding='utf-8', columns=header, index=False)
team_14.to_csv('data/Teams/Team14.csv', encoding='utf-8', columns=header, index=False)
# Print Success when complete
print("Success")
#######################################
#### Add Lahman Tag to Toster Names ###
########################################
# Goal is to find lahman tag for each rostered player
# if tag already exists skip
# if not found leave blank
#### Needed???
def Roster_lahman_tag():
roster = pd.read_csv('data/FullRoster.csv')
lahman = pd.read_csv('data/baseballdatabank-2019.2/core/People.csv')
print(roster.head())
# print(lahman.head())
# add in if clause that only completes the add in if the column is blank... this allows user changes to fix mistakes
####### replace the current_year with a global variable fromt he master file? or pull the date from the clock?
current_year = 2019 # current year
too_old = 50 # oldest a player could be and play
max_birth_year = current_year - too_old
# Filters out all player who have died and limits search to players who are not too_old
lahman = lahman[lahman.birthYear >= max_birth_year]
lahman = lahman[lahman.deathYear.isnull()]
# print(lahman.head())
# print(lahman.describe())
# searches lahman database for matches
for ind in roster.index:
Fname = roster['player_Fname'][ind]
Lname = roster['player_Lname'][ind]
pCheck = roster['lahmanID'][ind]
rCheck = roster['retroID'][ind]
bbCheck = roster['bbrefID'][ind]
match = lahman[(lahman.nameFirst == Fname) & (lahman.nameLast == Lname)].tail(1)
# print(match)
pID = match['playerID'].values
rID = match['retroID'].values
bbID = match['bbrefID'].values
# print(pID)
# print(rID)
# print(bbID)
# if statements to check if cell is already filled
# this allows for manual override if needed
if pd.isna(roster['lahmanID'][ind]):
roster["lahmanID"].iloc[ind]=pID
else:
pass
if pd.isna(roster['retroID'][ind]):
roster["retroID"].iloc[ind]=rID
else:
pass
if pd.isna(roster['bbrefID'][ind]):
roster["bbrefID"].iloc[ind]=bbID
else:
pass
#print(roster.head())
print(roster)
roster.to_csv('data/FullRoster.csv')
# Print Success when complete
print("Success")
# Pull Team csv files to roster.csv
# combine_Teams_to_Roster()
# Push roster.csv rosters to individual files
Rosters_To_Team_Files()
# Pulls Lahman Tags (playerID) for rosters (and: retroID bbrefID)
# Roster_lahman_tag()