iterrows() işleviyle döngü kurma

pandas ile Verimli Kod Yazma

Leonidas Souliotis

PhD Candidate

Poker veri kümesi

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. Kupa
  2. Karo
  3. Sinek
  4. Maça
pandas ile Verimli Kod Yazma

Python'da üreteçler (generator)

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

city_names = city_name_generator()
pandas ile Verimli Kod Yazma
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 ile Verimli Kod Yazma

iterrows() işleviyle döngü kurma

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 ile Verimli Kod Yazma

.iterrows() işlevini kullanma

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 ile Verimli Kod Yazma

Hadi yapalım!

pandas ile Verimli Kod Yazma

Preparing Video For Download...