dimanche 26 novembre 2023

generate random values from pd.dataframe with one condition

i have a given dataframe which looks like this:

data = {
  "Name": ["Mike", "Tom", "Marta", "Ole", "Jose", "Omar"],
  "Desk1": [1, 0, 1, 1, 1, 0],
  "Desk2": [0, 0, 1, 1, 0, 1],
  "Desk3": [1, 1, 1, 1, 1, 1],
  "Desk4": [1, 1, 1, 1, 1, 1],
  "Desk5": [0, 1, 0, 1, 0, 1],
  "Desk6": [1, 1, 1, 1, 1, 1],
}

1 is desk[x] is possible, 0 is desk[x] not possible.

I want to loop through the df 4x and generate a randomly picked list for each name looks like this: Mike = ["Desk2", "Desk4", "Desk6", "Desk3"] Tom = ["Desk5", "Desk3", "Desk4", "Desk6"] and so on...

Conditions:

  1. The desk only can be given in the first loop one time
  2. The desk should not be picked in the following loop.

Theoretically there are existing multiple possibities, which can be calculatet with different runs of the script.

I already created lists for Desk_total: Desk_total = ["Desk1", "Desk2", "Desk3", "Desk4", "Desk5", "Desk6"]

List for the names... TomDesk = ["Desk3", "Desk4", "Desk5", "Desk6"]

create empyt list like: Tom_rot = [] for each name

for r in range(1, 4):
   Desk_temp = Desk_total.copy()

   for m in range(len(df)):
       var = random.choice(TomDesk]) #here it sould be the pandas row number

       if (val is in Desk_temp) == True and (val is not Tom_rot[-1]) == True:
          Desk_temp.remove(val)
          Tom_rot.append[val]

and continue looping...

it stops at the second loop second latest and does not contiue. No matter the last Desk would match from Desk_temp to the "Omar_rot"-List

Is it possible to do that within the df or only with the loops as i tried?

Actually i think is a mathematical problem to solve.




Aucun commentaire:

Enregistrer un commentaire