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 每次循环(7 次运行的平均值 ± 标准差,每次 10 循环)
高效编写 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 每次循环(7 次运行的平均值 ± 标准差,每次 10 循环)
高效编写 Python 代码

用 .iterrows() 练习迭代 DataFrame

高效编写 Python 代码

Preparing Video For Download...