Reputation: 55
I'm writing code to randomly select people for a "lottery" drawing. Each person can be entered into the lottery multiple times; however, they can only be drawn once. The program will operate as a loop that runs X number of times. It'll select a person, append them to the variable "lucky winner" and then drop them from the dataframe so that they cannot be selected the next time the loop runs.
import pandas as pd
dataframe = pd.read_csv(r'csv file', header=None, delimiter=',')
lucky_winners = []
i=0
while i < 10:
lucky_winner = dataframe.sample(n=1)
lucky_winners.append(lucky_winner)
dataframe = dataframe.drop(lucky_winner[0].index)
i+=1
I have two issues 1) the drop function isn't working and 2) The winner could potentially be selected twice if all rows with the same label aren't dropped. How do I write the drop function so that it drops based on label and not index?
Thanks all!
Upvotes: 1
Views: 58
Reputation: 55
ended up going with this:
df= pd.read_csv(r'csv file', header=None, delimiter=',')
lucky_winner = df.sample(n=8,weights='weight')
Upvotes: 1
Reputation: 93191
Instead of looping and dropping from DataFrame, use weighted random:
import numpy as np
people = list('ABCD')
entries = np.array([3, 3, 2, 1])
entries = entries / entries.sum()
# Select 3 random winners from list above
winners = np.random.choice(people, 3, replace=False, p=entries)
Upvotes: 0