1

Is there a function in Python Pandas which randomly selects a row based on the values from a column in a dataframe (Function to perform roulette wheel selection)?

i.e.

 id     value
 1      1
 2      2
 3      3

Then with chance 1/(1+2+3) row 1 is selected, chance 2/6 row 2 and 3/6 row 3.

I know it is easy to write your own function with a random number between 0 and sum(value) and for loop and then select the row but I was wondering if there was a pre-defined function.

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
user3605780
  • 6,542
  • 13
  • 42
  • 67

1 Answers1

2

Select an index with something like:

i = choice(range(len(df)), df.value)

where choice is whatever you want from here.

Then use iloc.

Community
  • 1
  • 1
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185