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'.

>>> 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.

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
  • 1
    \$\begingroup\$ I'm not sure you can get a very meaningful review of a two-line function. But is there a good reason you need to pd.read_csv() every time it's called, rather than passing the dataframe as a parameter? \$\endgroup\$ Jul 22 at 13:56
  • 2
    \$\begingroup\$ Also, you introduce your code as "example" - we definitely need real code, in context to be able to review it effectively. \$\endgroup\$ Jul 22 at 14:15
  • \$\begingroup\$ @TobySpeight Maybe I should've asked this in StackOverFlow. The reason I don't pass the frame every time I need to use the function is because there are a number of instances where I need to return different aliases for teams and I'd rather have not have to type that line every time I need it. Does that make sense? I understand the methodology is off which is why I'm looking for some help. \$\endgroup\$
    – Nick
    Jul 22 at 15:03

1 Answer 1


First things first, a potential bug - you have alias('TEAM', 'RR'), but given the structure of the table, you probably want alias('TEAM', 'RW')

For a more elegant way of looking up stuff in a dataframe, realise that they are like a 2D dict, where one set of keys are the columns, and the other set is the index. Something like df.set_index('TEAM'); df.loc[team_name]['RW'], where team_name is the team name whose alias you want to look up, would solve your intermediate dict problem. You don't even need the set_index bit, as you already set the index when loading the CSV file, but setting the index with an explicit column name instead of an column number makes it easier to understand what's happening, and also insures you against some changes in the CSV file.

Rolling it all up,

def get_team_alias(team_name, alias_file = r'C:\Users\UserName\Teams.csv'):
    frame = pd.read_csv(alias_file, index_col='TEAM')
    return frame.loc[team_name, 'RW'] # frame.loc[<row_label_in_index>, <column_name>]

Not the answer you're looking for? Browse other questions tagged or ask your own question.