addicted
addicted

Reputation: 3051

python - Why do I get this Key Error:0 when using random.choice()?

I am trying to make a randomizer function to select random participants into a game. Basically my logic is:

  1. randomly select 1 name from dataframe column (using random.choice())
  2. black out that name by replacing it with NaN
  3. re-run

For my 2nd run, I am getting Key Error: 0 when using random.choice method. I can correct it by making it into a list using tolist().

Let me illustrate what I mean by following codes:

import pandas as pd
import numpy as np
from random import choice

df = pd.DataFrame({'name':['Andy', 'Jack', 'Anne']})
for i in range(1,100):
    c_name = choice(df.name.dropna())
df.name.replace(c_name, np.NaN, inplace=True) #Let's say Andy is replaced

# up until here it will work fine

# Trying the 2nd run

for i in range(1,100):
    c_name = choice(df.name.dropna())
# !will output Key Error : 0
# if I do this: c_name = choice(df.name.dropna().tolist()) it will be fine.
# if the NaN value is at the last index, it is also fine.

My question is why random.choice() behaves like this? I have dropped the NaN value using dropna(), it should act as a Series of names without NaN.

Upvotes: 1

Views: 1455

Answers (1)

chrisckwong821
chrisckwong821

Reputation: 1173

The first error is:

result = self.index.get_value(self, key)

Since random.choice takes a sequence:

random.choice(seq) Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

so given a dataframe it would implicitly change the df into a list by each index. It throws an error if the index is not continuous.

Upvotes: 2

Related Questions