0

In one part of my project , I need to create a random month-names and store them into a data-frame column. currently I am using the following snippet: First, Creating a data-frame of predefined size:

df = pd.DataFrame(index=range(size))

then creating 120 random Time-Stamp and storing them into ['Timestamp'] column:

df["Timestamp"] = [ pd.Timestamp(2017, np.random.randint(1,13), 1) for _ in range(120) ] 

at the end extracting the Months and stroing them into ['STD_Months'] column :

df["STD_Months"] = df["Timestamp"].apply(lambda x: x.strftime('%B'))

this creates random months but with different quantity , I mean we may have 10 January out of 120 samples , 14 May , 8 December etc(Not equal quantity)

How can I modify my code to have the same quantity of random samples(10 instances of each month name:10 January , 10 February , .... ,10 December)

1 Answer 1

1

One way is to create a non-random list and then shuffle it:

import random

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
months *= 10
random.shuffle(months)

Then just use months as the column.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks John, I was also thinking about a simple solution like this @John Coleman

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.