Iterando com a função .iterrows()

Escrevendo código eficiente com pandas

Leonidas Souliotis

PhD Candidate

O conjunto de dados de pôquer

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. Copas
  2. Ouros
  3. Paus
  4. Espadas
Escrevendo código eficiente com pandas

Geradores em Python

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

city_names = city_name_generator()
Escrevendo código eficiente com 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
Escrevendo código eficiente com pandas

Iterando com a função .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
Escrevendo código eficiente com pandas

Usando a função .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
Escrevendo código eficiente com pandas

Vamos praticar!

Escrevendo código eficiente com pandas

Preparing Video For Download...