.iloc को अंडरलाईंग arrays से बदलना
अब जब आपको DataFrame के इंटरनल्स का बेहतर अंदाज़ा है, तो आइए अपनी एक पुरानी एनालिसिस को अपडेट करें ताकि DataFrame की अंडरलाईंग arrays का फायदा उठाया जा सके। आप उस win percentage कैलकुलेशन पर लौटेंगे जिसे आपने .iloc मेथड के साथ row-by-row चलाया था:
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
आइए इस एनालिसिस को .iloc मेथड की जगह arrays का उपयोग करने के लिए अपडेट करें। एक DataFrame (baseball_df) आपके सेशन में लोड कर दिया गया है।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Efficient Python Code लिखना
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Use the W array and G array to calculate win percentages
win_percs_np = calc_win_perc(baseball_df[____].____, baseball_df[____].____)