[Python] Optimal Grouping Strategy to Equalize Column Sums with String Aggregation

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataset with two columns: sID (string identifier) and sum_count (numeric value). My goal is to partition these sIDs into 5 groups with the following requirements:


    1. The sum of sum_count values in each group should be as equal as possible (exact equality is not required, but close approximations are preferred).


    2. Within each group, the sIDs should be merged into a comma-separated string.

    Example Input:

    sID sum_count
    A 10
    B 5
    C 8
    D 12
    E 9
    F 13


    Expected Output Format:

    Group1: sID=A,C,E | sum_count_total=27
    Group2: sID=B,D,F | sum_count_total=30
    ... # Continuing for 5 groups


    I tried naive methods like:

    • Random sampling
    • Sorting by sum_count followed by round-robin/cyclic distribution But these produced imbalanced group sums (e.g., ±20% variance).

    Is there a more efficient algorithm or existing tool (e.g., Python library, DolphinDB function) to achieve this?

    Continue reading...

Compartilhe esta Página