简体   繁体   中英

Dataframe task distribution basing on conditions changing during distribution

Let's consider two separate dataframes:

DF1:

ID    Name    Surname    Total_Value    [further, not important as of now booleans etc.]
1     John    Smith      1.0
2     Adam    Johnson    0.95

DF2:

Task_Name    Value    Person_ID
[...]        [...]    [...]
Task_132     0.08     None

This is a very simplified version of a problem that I have, so if that would matter in responses please let me know. What I'm trying to do is to distribute all tasks from DF2 to a person having the least Total_Value in DF1 . Obviously, when I assign Task#132 to Adam Johnson, he's going to have bigger Total_Value, so next task is going to be distributed to another person.

As of now, I've solved that with a terrible VBA code which basically loops through each element in DF2 and then again loops through all DF1 elements (up to 10 times, to find the best possible fit). When there's ~200 elements in DF1 and 4000 elements in DF2 it takes way too long and is simply not a good solution in case of future changes.

I do believe there's a better way to do this in Python, but I'm not sure how to go about that without using multiple loops and redefining DataFrames along the way.

EDIT: Still didn't came up with any other solution and I'm wondering if that's possible at all, considering the silence.

EDIT2: To be frank, it doesn't even need to use DataFrames. If there's any other way to do that, I can adjust.

I think you can make querying the smallest element in DF1 a lot faster if you implement it as a Priorityqueue for example using heapq . These data structures allow extracting the smallest element and inserting/updating an element in O(log n) time. You would still need to loop through the tasks in DF2, but updating would be much faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM