Issue
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.
Solution
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
Answered By - user7864386
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.