[Python] Sort Pandas Dataframe by repeating Pattern

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pandas dataset I would like to sort in a semi-random way. The data contains names and I need to sort them in a pattern so that the names are cycled through before being repeated, and I would also love for that order to be random. When working down the dataset I need to encounter each name as evenly as possible as I do not always get through the entire list. When I have the list randomized, the data set is hundreds of lines long I often don't encounter the less frequent names as I only expect to check the first 20ish% of the data and need an equal chance of encountering each name regardless of how often they are in the list. I would like the order of the names to be random so the same name isn't always choosen first with a fresh dataset if that's possible. Also I need to keep every entry in the list and can not drop lines.

    For example

    { 'Name' : ['Sam', 'John', 'Amy', 'Sam', 'Sam', 'Amy', 'Sam', 'Amy', 'Sarah', 'John', 'Sam'],

    'Score' :['2','3','5','4','6','10','8','15','7','9', '12'] }

    Name Score
    1 Sarah 7
    2 Amy 15
    3 Sam 4
    4 John 3
    5 Amy 5
    6 Sam 2
    7 John 9
    8 Amy 10
    9 Sam 8
    10 Sam 12
    11 Sam 6

    Continue reading...

Compartilhe esta Página