Writing Efficient Code with 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 |
def city_name_generator():
yield('New York')
yield('London')
yield('Tokyo')
yield('Sao Paolo')
city_names = city_name_generator()
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
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
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
Writing Efficient Code with pandas