在Pandas中通过多处理读取csv文件的最简单方法

在Pandas中通过多处理读取csv文件的最简单方法,第1张

在Pandas中通过多处理读取csv文件的最简单方法

使用

Pool

import osimport pandas as pd from multiprocessing import Pool# wrap your csv importer in a function that can be mappeddef read_csv(filename):    'converts a filename to a pandas dataframe'    return pd.read_csv(filename)def main():    # get a list of file names    files = os.listdir('.')    file_list = [filename for filename in files if filename.split('.')[1]=='csv']    # set up your pool    with Pool(processes=8) as pool: # or whatever your hardware can support        # have your pool map the file names to dataframes        df_list = pool.map(read_csv, file_list)        # reduce the list of dataframes to a single dataframe        combined_df = pd.concat(df_list, ignore_index=True)if __name__ == '__main__':    main()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5644978.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存