-1
我在我的数据框中有5,000,000行。在我的代码中,我使用了iterrows(),这花费了太多时间。为了获得所需的输出,我必须遍历所有行。所以我想知道我是否可以在熊猫中并行化代码。熊猫并行处理python
我在我的数据框中有5,000,000行。在我的代码中,我使用了iterrows(),这花费了太多时间。为了获得所需的输出,我必须遍历所有行。所以我想知道我是否可以在熊猫中并行化代码。熊猫并行处理python
这里有一个网页,我发现可能会帮助:http://gouthamanbalaraman.com/blog/distributed-processing-pandas.html
下面是在页面发现多代码:
import pandas as pd
import multiprocessing as mp
LARGE_FILE = "D:\\my_large_file.txt"
CHUNKSIZE = 100000 # processing 100,000 rows at a time
def process_frame(df):
# process data frame
return len(df)
if __name__ == '__main__':
reader = pd.read_table(LARGE_FILE, chunksize=CHUNKSIZE)
pool = mp.Pool(4) # use 4 processes
funclist = []
for df in reader:
# process each data frame
f = pool.apply_async(process_frame,[df])
funclist.append(f)
result = 0
for f in funclist:
result += f.get(timeout=10) # timeout in 10 seconds
print "There are %d rows of data"%(result)
矢量化parrallelize之前!你可以通过避免iterrows()来在熊猫中进行矢量化。如果你一直在使用iterrows,你可能没有足够的时间学习熊猫的基础知识。先做。之后,询问显示代码的特定问题以及一些示例数据。 – JohnE