I have a Pandas data frame (called "ud_flex" below) that looks like the one below:
The data frame has over 27 million observations in it that I'm trying to iterate through to do a calculation for each row. Below is the calculation that I'm using:
def set_fpts(pos, rank, curr_fpts):
if pos == "RB" and rank >= 3.0:
return 0
elif pos == "WR" and rank >= 4.0:
return 0
elif (pos == "TE" or pos == "QB") and rank >= 2.0:
return 0
else:
return curr_fpts
Here is the for loop that I've created:
players = ud_flex.shape[0]
for i in range(0,players):
new_fpts = set_fpts(ud_flex.iloc[i]['position_name'], ud_flex.iloc[i]['wk_rank_orig'], ud_flex.iloc[i]['fpts'])
ud_flex.at[i, 'fpts_orig'] = new_fpts
Does anyone have any suggestions for how to speed up this loop? It's currently taking nearly an hour! Thanks!