การวนซ้ำด้วยฟังก์ชัน .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. Hearts
  2. Diamonds
  3. Clubs
  4. Spades
การเขียนโค้ดที่มีประสิทธิภาพด้วย pandas

Generator ใน Python

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...