Pandas の Series でのベクトル化

pandasで効率的にコードを書く

Leonidas Souliotis

PhD Candidate

配列としての DataFrame

GitHub ロゴ

pandasで効率的にコードを書く

pandas ベクトル化の実行方法

start_time = time.time()
poker[['R1', 'R2', 'R3', 'R4', 'R5']].sum(axis=1)
print("Time using pandas vectorization: {} sec".format(time.time() - start_time))
Time using pandas vectorization: 0.0026819705 sec
poker[['R1', 'R2', 'R3', 'R4', 'R5']].sum(axis=1).head()
|              |    | 
|--------------|----| 
| 0            | 47 | 
| 1            | 47 | 
| 2            | 47 | 
| 3            | 47 | 
| 4            | 47 | 
| dtype: int64 | -- |
pandasで効率的にコードを書く

既存手法との比較

data_generator = data.iterrows()

start_time = time.time()
for index, value in data_generator:
        sum([value[1], value[3], value[5], value[7]])
print("Time using .iterrows(){} seconds" % (time.time() - start_time))
上記の処理は 3.37918996 秒で完了
start_time = time.time()
data[['R1', 'R2', 'R3', 'R4', 'R5']].apply(lambda x: sum(x),axis=1)
print("Results from the above operation calculated in %s seconds" % (time.time() - start_time))
上記の処理は 0.637711048 秒で完了
- ベクトル化と`.iterrows()`の差: 111,800.75%
- ベクトル化と`.apply()`の差: 20,853%
pandasで効率的にコードを書く

やってみましょう!

pandasで効率的にコードを書く

Preparing Video For Download...