For a long time I've used Python strictly to get something done even though I knew there was likely a much more "pythonic" way of accomplishing a given task. In the example below I have a table that is saved as a CSV and when I need to return an alias I use the alias function.
def alias(old,new):
frame = pd.read_csv(r'C:\Users\UserName\Teams.csv', index_col=[0])
return dict(zip(frame[old],frame[new]))
FWIW I know one way of improving speed would be to save the data as a dictionary. The only reason I haven't done so is because it's easier for me to make quick additions/changes by opening the Excel file but that's not a deal breaker. That being said, does anyone have ideas on how to improve on this code? And more specifically, is there potential to make it so I only have to indicate which alias I'm looking to return? For example, right now I have to use this line of code to return the RR value for 'ARI'.
alias('TEAM','RR').get('ARI')
>>> 15
What would be great is if I didn't have to specify that I'm referencing the 'TEAM' value and instead just indicate that I'm looking to return the 'RR' value for a given team/teams. I feel like this should be possible especially given the fact that there are no duplicates. Any thoughts on this would be greatly appreciated.
TEAM | MLB | MLB_ID | RW | RR | DBD | FULL_NAME |
---|---|---|---|---|---|---|
ARI | ari | 109 | ARZ | 15 | Arizona Diamondbacks | Arizona Diamondbacks |
ATL | atl | 144 | ATL | 16 | Atlanta Braves | Atlanta Braves |
BAL | bal | 110 | BAL | 2 | Baltimore Orioles | Baltimore Orioles |
BOS | bos | 111 | BOS | 3 | Boston Red Sox | Boston Red Sox |
CHC | chc | 112 | CHC | 17 | Chicago Cubs | Chicago Cubs |
CHW | cws | 145 | CHW | 4 | Chicago White Sox | Chicago White Sox |
CIN | cin | 113 | CIN | 18 | Cincinnati Reds | Cincinnati Reds |
CLE | cle | 114 | CLE | 5 | Cleveland Guardians | Cleveland Indians |
COL | col | 115 | COL | 19 | Colorado Rockies | Colorado Rockies |
DET | det | 116 | DET | 6 | Detroit Tigers | Detroit Tigers |
HOU | hou | 117 | HOU | 21 | Houston Astros | Houston Astros |
KCR | kc | 118 | KC | 7 | Kansas City Royals | Kansas City Royals |
LAA | ana | 108 | LAA | 1 | LA Angels | Los Angeles Angels |
LAD | la | 119 | LAD | 22 | LA Dodgers | Los Angeles Dodgers |
MIA | mia | 146 | MIA | 20 | Miami Marlins | Miami Marlins |
MIL | mil | 158 | MIL | 23 | Milwaukee Brewers | Milwaukee Brewers |
MIN | min | 142 | MIN | 8 | Minnesota Twins | Minnesota Twins |
NYM | nym | 121 | NYM | 25 | New York Mets | New York Mets |
NYY | nyy | 147 | NYY | 9 | New York Yankees | New York Yankees |
OAK | oak | 133 | OAK | 10 | Oakland Athletics | Oakland Athletics |
PHI | phi | 143 | PHI | 26 | Philadelphia Phillies | Philadelphia Phillies |
PIT | pit | 134 | PIT | 27 | Pittsburgh Pirates | Pittsburgh Pirates |
SDP | sd | 135 | SD | 29 | San Diego Padres | San Diego Padres |
SEA | sea | 136 | SEA | 11 | Seattle Mariners | Seattle Mariners |
SFG | sf | 137 | SF | 30 | San Francisco Giants | San Francisco Giants |
STL | stl | 138 | STL | 28 | St. Louis Cardinals | St. Louis Cardinals |
TBR | tb | 139 | TB | 12 | Tampa Bay Rays | Tampa Bay Rays |
TEX | tex | 140 | TEX | 13 | Texas Rangers | Texas Rangers |
TOR | tor | 141 | TOR | 14 | Toronto Blue Jays | Toronto Blue Jays |
WSN | was | 120 | WAS | 24 | Washington Nationals | Washington Nationals |
pd.read_csv()
every time it's called, rather than passing the dataframe as a parameter? \$\endgroup\$