使用 .iterrows() 迴圈處理

使用 pandas 撰寫高效程式碼

Leonidas Souliotis

PhD Candidate

撲克資料集

S1 R1 S2 R2 S3 R3 S4 R4 S5 R5
1 1 10 3 11 3 13 4 4 2 1
2 2 11 2 13 2 10 2 12 2 1
3 3 12 3 11 3 13 3 10 3 1
  1. 紅心
  2. 方塊
  3. 梅花
  4. 黑桃
使用 pandas 撰寫高效程式碼

Python 的產生器(generator)

def city_name_generator():
    yield('New York')
    yield('London')
    yield('Tokyo')
    yield('Sao Paolo')

city_names = city_name_generator()
使用 pandas 撰寫高效程式碼
next(city_names)
'New York'
next(city_names)
'London'
next(city_names)
'Tokyo'
next(city_names)
'Sao Paolo'
next(city_names)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
使用 pandas 撰寫高效程式碼

使用 .iterrows() 迴圈處理

gen = poker.iterrows()
first_element = next(gen)
first_element[0]
0
first_element[1]
S1     2
R1    11
S2     2
R2    13
S3     2
R3    10
S4     2
R4    12
S5     2
R5     1
Name: 1, dtype: int64
使用 pandas 撰寫高效程式碼

使用 .iterrows() 函式

start_time = time.time()
for index, values in range(poker.shape[0]):
    next
print("Time using range(): {} sec".format(time.time() - start_time))
Results using range(): 0.006870031 sec
data_generator = poker.iterrows()

start_time = time.time()
for index, values in data_generator:
    next
print("Time using .iterrows(): {} sec".format(time.time() - start_time))
Time using .iterrows(): 1.55003094673 sec
使用 pandas 撰寫高效程式碼

一起來練習吧!

使用 pandas 撰寫高效程式碼

Preparing Video For Download...