แนะนำการวนซ้ำ pandas DataFrame

การเขียน Python Code ที่มีประสิทธิภาพ

Logan Thomas

Scientific Software Technical Trainer, Enthought

ทบทวน pandas

  • ดูภาพรวม pandas ใน Intermediate Python
  • ไลบรารีสำหรับการวิเคราะห์ข้อมูล
  • โครงสร้างข้อมูลหลักคือ DataFrame
    • ข้อมูลตารางที่มีแถวและคอลัมน์มีชื่อกำกับ
    • สร้างบน NumPy array
  • เป้าหมายของบท:
    • แนวปฏิบัติที่ดีที่สุดสำหรับการวนซ้ำ pandas DataFrame
การเขียน Python Code ที่มีประสิทธิภาพ

สถิติเบสบอล

import pandas as pd

baseball_df = pd.read_csv('baseball_stats.csv')
print(baseball_df.head())
  Team League  Year   RS   RA   W    G  Playoffs
0  ARI     NL  2012  734  688  81  162         0
1  ATL     NL  2012  700  600  94  162         1
2  BAL     AL  2012  712  705  93  162         1
3  BOS     AL  2012  734  806  69  162         0
4  CHC     NL  2012  613  759  61  162         0
การเขียน Python Code ที่มีประสิทธิภาพ

สถิติเบสบอล

  Team
0  ARI     
1  ATL     
2  BAL     
3  BOS     
4  CHC

alt="โลโก้ Arizona Diamondbacks มีข้อความ ARI ด้านล่าง, โลโก้ Atlanta Braves มีข้อความ ATL ด้านล่าง, โลโก้ Baltimore Orioles มีข้อความ BAL ด้านล่าง, โลโก้ Boston Red Sox มีข้อความ BOS ด้านล่าง และโลโก้ Chicago Cubs มีข้อความ CHC ด้านล่าง"

การเขียน Python Code ที่มีประสิทธิภาพ

สถิติเบสบอล

  Team League  Year   RS   RA   W    G  Playoffs
0  ARI     NL  2012  734  688  81  162         0
1  ATL     NL  2012  700  600  94  162         1
2  BAL     AL  2012  712  705  93  162         1
3  BOS     AL  2012  734  806  69  162         0
4  CHC     NL  2012  613  759  61  162         0
การเขียน Python Code ที่มีประสิทธิภาพ

คำนวณเปอร์เซ็นต์ชนะ

import numpy as np

def calc_win_perc(wins, games_played):

    win_perc = wins / games_played

    return np.round(win_perc,2)
win_perc = calc_win_perc(50, 100)
print(win_perc)
0.5
การเขียน Python Code ที่มีประสิทธิภาพ

เพิ่มเปอร์เซ็นต์ชนะลงใน DataFrame

win_perc_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_perc_list.append(win_perc)
baseball_df['WP'] = win_perc_list
การเขียน Python Code ที่มีประสิทธิภาพ

เพิ่มเปอร์เซ็นต์ชนะลงใน DataFrame

print(baseball_df.head())
  Team League  Year   RS   RA   W    G  Playoffs    WP
0  ARI     NL  2012  734  688  81  162         0  0.50
1  ATL     NL  2012  700  600  94  162         1  0.58
2  BAL     AL  2012  712  705  93  162         1  0.57
3  BOS     AL  2012  734  806  69  162         0  0.43
4  CHC     NL  2012  613  759  61  162         0  0.38
การเขียน Python Code ที่มีประสิทธิภาพ

การวนซ้ำด้วย .iloc

%%timeit
win_perc_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_perc_list.append(win_perc)

baseball_df['WP'] = win_perc_list
183 ms ± 1.73 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
การเขียน Python Code ที่มีประสิทธิภาพ

การวนซ้ำด้วย .iterrows()

win_perc_list = []

for i,row in baseball_df.iterrows():

wins = row['W'] games_played = row['G'] win_perc = calc_win_perc(wins, games_played) win_perc_list.append(win_perc) baseball_df['WP'] = win_perc_list
การเขียน Python Code ที่มีประสิทธิภาพ

การวนซ้ำด้วย .iterrows()

%%timeit
win_perc_list = []

for i,row in baseball_df.iterrows():

    wins = row['W']
    games_played = row['G']

    win_perc = calc_win_perc(wins, games_played)
    win_perc_list.append(win_perc)

baseball_df['WP'] = win_perc_list
95.3 ms ± 3.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
การเขียน Python Code ที่มีประสิทธิภาพ

มาฝึกกันเถอะ!

การเขียน Python Code ที่มีประสิทธิภาพ

Preparing Video For Download...