[Python] Random sample with multi-variate conditions

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataset that I need to pull a random sample from. This is the data:

    {'character': {0: 'mario',
    1: 'luigi',
    2: 'yoshi',
    3: 'peach',
    4: 'bowser',
    5: 'boo',
    6: 'toad',
    7: 'lakitu',
    8: 'koopa'},
    'location': {0: 'castle',
    1: 'castle',
    2: 'forest',
    3: 'castle',
    4: 'dungeon',
    5: 'dungeon',
    6: 'forest',
    7: 'forest',
    8: 'dungeon'},
    'color': {0: 'red',
    1: 'green',
    2: 'green',
    3: 'yellow',
    4: 'blue',
    5: 'white',
    6: 'blue',
    7: 'yellow',
    8: 'green'}}


    Which I have arranged a data frame named df1.

    What I need to do is pull a random sample of characters from the dataframe, making sure that the random sample has at least 2 (if possible) characters from each location and at least 1 (if possible) characters from each color

    Ordinarily I would use df1.sample(n = 2), but in this case there are two criteria that need to be met, and n = 2 only applies to the location criteria, not the color criteria

    Is it possible to do all of this in one function?

    Thanks!

    Continue reading...

Compartilhe esta Página