How to choose 3 random numbers from a pandas.series object?

Viewed 43

I have a pandas.core.series.Series object that looks like this:

6          7
8          9
18        19
35        36
42        43

I want to get a list of 3 randomly chosen numbers from this list. I tried following the advice here and tried

sampled_list = random.sample(df['ID'], 3)

with no luck. Any suggestions?

2 Answers

I think you're looking for:

df['ID'].sample(3).tolist()

Docs: Series.sample()

sampled_list = random.sample(list(df['ID']), 3)
Related