pandas DataFrame 迭代入門

撰寫高效的 Python 程式碼

Logan Thomas

Scientific Software Technical Trainer, Enthought

pandas 複習

  • Intermediate Python 中回顧 pandas
  • 用於資料分析的函式庫
  • 主要資料結構為 DataFrame
    • 具有標籤的列與欄的表格資料
    • 建構於 NumPy 陣列結構之上
  • 本章目標:
    • 以最佳實務迭代 pandas DataFrame
撰寫高效的 Python 程式碼

棒球統計資料

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 程式碼

棒球統計資料

  Team
0  ARI     
1  ATL     
2  BAL     
3  BOS     
4  CHC

alt="亞利桑那響尾蛇隊徽,下方文字 ARI;亞特蘭大勇士隊徽,下方文字 ATL;巴爾的摩金鶯隊徽,下方文字 BAL;波士頓紅襪隊徽,下方文字 BOS;芝加哥小熊隊徽,下方文字 CHC"

撰寫高效的 Python 程式碼

棒球統計資料

  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 程式碼

計算勝率

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 程式碼

將勝率加入 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 程式碼

將勝率加入 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 程式碼

使用 .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 程式碼

使用 .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 程式碼

使用 .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 程式碼

用 .iterrows() 練習 DataFrame 迭代

撰寫高效的 Python 程式碼

Preparing Video For Download...