การวนซ้ำใน pandas อย่างเหมาะสม

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

Logan Thomas

Scientific Software Technical Trainer, Enthought

โครงสร้างภายในของ pandas

  • การกำจัดลูปใช้ได้กับ pandas ด้วย
  • pandas สร้างบน NumPy
    • ใช้ประโยชน์จากประสิทธิภาพของ NumPy array

alt="โลโก้ไลบรารี pandas อยู่บนโลโก้แพ็กเกจ NumPy ซึ่งอยู่บนโลโก้ภาษา Python โดยมีเส้นจาก pandas ไป NumPy พร้อมข้อความ Builds on และเส้นจาก NumPy ไป Python พร้อมข้อความ Builds on"

การเขียน Python Code ที่มีประสิทธิภาพ
print(baseball_df)
  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
...
wins_np = baseball_df['W'].values

print(type(wins_np))
<class 'numpy.ndarray'>
print(wins_np)
[ 81  94  93 ...]
การเขียน Python Code ที่มีประสิทธิภาพ

พลังของ vectorization

  • Broadcasting (vectorizing) มีประสิทธิภาพสูงมาก!
baseball_df['RS'].values - baseball_df['RA'].values
array([  46,  100,    7, ...,  188,  110, -117])
การเขียน Python Code ที่มีประสิทธิภาพ

ส่วนต่างของรันด้วย array

run_diffs_np = baseball_df['RS'].values - baseball_df['RA'].values

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

เปรียบเทียบแต่ละแนวทาง

%%timeit
run_diffs_np = baseball_df['RS'].values - baseball_df['RA'].values

baseball_df['RD'] = run_diffs_np
124 µs ± 1.47 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
การเขียน Python Code ที่มีประสิทธิภาพ

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

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

Preparing Video For Download...