gpt4 book ai didi

python - 冗长的 Pandas 适用

转载 作者:太空宇宙 更新时间:2023-11-04 08:03:56 29 4
gpt4 key购买 nike

我正在对一个 Pandas 系列进行相对繁重的应用。有什么方法可以返回一些打印反馈,说明每次调用函数时在函数内部进行打印还有多远?

最佳答案

您可以使用跟踪器包装您的函数。以下两个示例,一个基于完成的迭代次数,一个基于总工作量的百分比。

from pandas import Series

def progress_coroutine(print_on = 10000):
print "Starting progress monitor"

iterations = 0
while True:
yield
iterations += 1
if (iterations % print_on == 0):
print "{} iterations done".format(iterations)

def percentage_coroutine(to_process, print_on_percent = 0.10):
print "Starting progress percentage monitor"

processed = 0
count = 0
print_count = to_process*print_on_percent
while True:
yield
processed += 1
count += 1
if (count >= print_count):
count = 0
pct = (float(processed)/float(to_process))*100

print "{}% finished".format(pct)

def trace_progress(func, progress = None):
def callf(*args, **kwargs):
if (progress is not None):
progress.send(None)

return func(*args, **kwargs)

return callf

def my_func(i):
return i * 2

data_series = Series(xrange(100000))
co1 = progress_coroutine()
co1.next()
co2 = percentage_coroutine(len(data_series))
co2.next()
data_series.apply(trace_progress(my_func, progress = co1))
data_series.apply(trace_progress(my_func, progress = co2))

Starting progress monitor
Starting progress percentage monitor
10000 iterations done
20000 iterations done
30000 iterations done
40000 iterations done
50000 iterations done
60000 iterations done
70000 iterations done
80000 iterations done
90000 iterations done
100000 iterations done
10.0% finished
20.0% finished
30.0% finished
40.0% finished
50.0% finished
60.0% finished
70.0% finished
80.0% finished
90.0% finished
100.0% finished

关于python - 冗长的 Pandas 适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092720/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com