多线程数据帧合并python

2024-04-25 22:36:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚接触多线程。幸运的是,我的代码部分工作正常。我遇到的问题是,其他文件中的一些数据帧没有合并到组合的数据帧中

有没有可能的解决办法?多谢各位

import os, pandas as pd
import threading

files = os.listdir(os.getcwd())

df = pd.DataFrame()
def merging(f):
    #shortened
    data = pd.read_excel(f)
    df = df.append(data, sort=False).reset_index(drop=True)

threads = []
for f in files:
    thread1 = threading.Thread(target=merging, args=(f,))
    thread1.start()
    threads.append(thread1)

for thread in threads:
    thread.join()

Tags: 数据inimportdffordataosfiles
1条回答
网友
1楼 · 发布于 2024-04-25 22:36:37

我能够阅读队列以返回线程的值:

import os, pandas as pd
import threading, queue

files = os.listdir(os.getcwd())

df = pd.DataFrame()
def merging(f):
    #shortened
    data = pd.read_excel(f)
    return data

FILE_QUEUE = queue.Queue()
THREADS = []

for f in files:
    thread = threading.Thread(target=lambda q, arg1: q.put(merging(arg1)), args=(FILE_QUEUE, f))
    thread.start()
    THREADS.append(thread)

for thread in THREADS:
    thread.join()

while not FILE_QUEUE.empty():
    data = FILE_QUEUE.get()
    df = df.append(data, sort=False).reset_index(drop=True)

相关问题 更多 >