Replacing .iloc with underlying arrays
Now that you have a better grasp on a DataFrame's internals let's update one of your previous analyses to leverage a DataFrame's underlying arrays. You'll revisit the win percentage calculations you performed row by row with the .iloc
method:
def calc_win_perc(wins, games_played):
win_perc = wins / games_played
return np.round(win_perc,2)
win_percs_list = []
for i in range(len(baseball_df)):
row = baseball_df.iloc[i]
wins = row['W']
games_played = row['G']
win_perc = calc_win_perc(wins, games_played)
win_percs_list.append(win_perc)
baseball_df['WP'] = win_percs_list
Let's update this analysis to use arrays instead of the .iloc
method. A DataFrame (baseball_df
) has been loaded into your session.
This exercise is part of the course
Writing Efficient Python Code
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Use the W array and G array to calculate win percentages
win_percs_np = calc_win_perc(baseball_df[____].____, baseball_df[____].____)