mardi 20 décembre 2022

Python pandas conditional random select - selecting random item with multiple criteria

Trying to make a random selection from a list of products:

product_id  type    year_created
1   shirt   2021
2   book    2021
3   chair   2022
4   shirt   2021
5   book    2022
6   shirt   2022
7   shirt   2022
8   desk    2021
9   shirt   2022
10  lamp    2022
11  tv  2021
12  tv  2022

...

Would like to select random product ids, but making sure that the end result has one of each "type" in each "year released". So only one shirt from 2021, one shirt from 2022, one book from 2021, one book from 2022, etc.

Is there a way to do this in Python with a function apart from filtering the data and running a random query on each subset? Thank you!




Aucun commentaire:

Enregistrer un commentaire