pandas DataFrame iteration परिचय

Efficient Python Code लिखना

Logan Thomas

Scientific Software Technical Trainer, Enthought

pandas रिकैप

  • Intermediate Python में pandas का ओवरव्यू देखें
  • डेटा एनालिसिस के लिए लाइब्रेरी
  • मुख्य डेटा स्ट्रक्चर है DataFrame
    • लेबल्ड पंक्तियों और कॉलम वाला टेब्युलर डेटा
    • NumPy array स्ट्रक्चर पर आधारित
  • अध्याय का लक्ष्य:
    • pandas DataFrame पर इटरेट करने की बेहतरीन विधि
Efficient Python Code लिखना

Baseball आँकड़े

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
Efficient Python Code लिखना

Baseball आँकड़े

  Team
0  ARI     
1  ATL     
2  BAL     
3  BOS     
4  CHC

alt="एरिज़ोना डायमंडबैक्स लोगो, नीचे ARI; अटलांटा ब्रेव्स लोगो, नीचे ATL; बाल्टीमोर ओरीओल्स लोगो, नीचे BAL; बोस्टन रेड सॉक्स लोगो, नीचे BOS; और शिकागो कबस लोगो, नीचे CHC"

Efficient Python Code लिखना

Baseball आँकड़े

  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
Efficient 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
Efficient 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
Efficient 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
Efficient 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)
Efficient 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
Efficient 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)
Efficient Python Code लिखना

अभ्यास: .iterrows() से DataFrame पर इटरेट करें

Efficient Python Code लिखना

Preparing Video For Download...