[Python] Sorting grouped individuals into defined spaces [closed]

Discussão em 'Python' iniciado por Stack, Novembro 22, 2025.

  1. Stack

    Stack Membro Participativo

    Not sure if this is the right place get help with this kind of problem, but I am trying to solve this by coding so I'll try. If anyone knows about some better suited forum/place for this kind of problem, let me know.

    I have a data frame that looks like this:

    import pandas as pd


    data = {
    "ID": [1, 2, 3, 4, 5, 6, 7....],
    "NAME": ["male_1", "male_2", "male_3", "male_4", "female_1", "female_2", "female_3"...],
    "M_F": ["M", "M", "M", "M", "F", "F", "F"...],
    "AGE": [9, 5, 8, 6, 10, 11, 9...],
    "GROUP": ["1,2,3", "1,2,3", "1,2,3", pd.NA, "5,6", "5,6", pd.NA...]
    }


    df = pd.DataFrame(data)



    In r it can be written as

    df <- data.frame(
    ID = c(1, 2, 3, 4, 5, 6, 7...),
    NAME = c("male_1", "male_2", "male_3", "male_4", "female_1", "female_2", "female_3"...),
    M_F = c("M", "M", "M", "M", "F", "F", "F"...),
    AGE = c(9, 5, 8, 6, 10, 11, 9..),
    GROUP = c("1,2,3", "1,2,3", "1,2,3", NA, "5,6", "5,6", NA...),
    stringsAsFactors = FALSE
    )


    And it something like this

    | ID | NAME | M_F | AGE | GROUP |
    | -- | ---- | --- | --- | ----- |
    | 1 |male_1 | M| 9 | 1,2,3|
    | 2 |male_2 | M| 5 | 1,2,3|
    | 3 |male_3 | M| 8 | 1,2,3|
    | 4 |male_4 | M| 6 | NA |
    | 5 |female_1 |F|10|5,6|
    | 6 |female_2 |F|11|5,6|
    | 7 |female_3 |F|9|NA|
    | . | . | . | . | .|


    I have about 100 people in this data frame(length of DataFrame) , grouped as indicated by GROUP category in groups of up to 7 and some have no group (NAs). I need to place these people into huts/houses which can house 3, 4, 7 or 8 people. Number of these huts are as follows:

    • 4x hut with size 3
    • 13x hut with size 4
    • 4x hut with size 7
    • 4x hut with size 8

    So I have a total housing capacity of 124(4 * 3 + 13 * 4 + 4 * 7 + 4 * 8) people for about 100 people, so there is some spare space. Criteria for placing people in these huts are as follows:

    1. Males and females have to be in separate huts
    2. Groups have to be respected, people with group = NA can be placed with any group, in any hut
    3. Difference in age between youngest and oldest in hut should be 4 years maximum

    I am working on this problem in R, but I have little experience with Python as well, if that would be more comfortable for someone. Any help or ideas are appreciated. It is a passion project of mine, I am just trying to help out with some tedious task of manualy sorting these kids into huts on a summer camp :D
    Cheers!

    Continue reading...

Compartilhe esta Página