2

I have a data frame with the customers as shown below.

df:

id      name     
1       john
2       dan
3       sam

also, I have a list as

['www.costco.com', 'www.walmart.com']

I would like to add a column named domain to df by randomly selecting the elements from the list.

Expected output:

id      name       domain
1       john       www.walmart.com
2       dan        www.costco.com
3       sam        www.costco.com

Note: since it is a random selection output may not be the same as always.

It is randomly selecting from the given list of strings, hence it is not same and not duplicate. And it is a specific question and it got great and very specific answers.

Danish
  • 2,719
  • 17
  • 32

1 Answers1

6

You can use random.choices:

import random
df['domain'] = random.choices(lst, k=len(df))

A sample output:

   id  name           domain
0   1  john  www.walmart.com
1   2   dan   www.costco.com
2   3   sam   www.costco.com