John Doe
John Doe

Reputation: 557

How do I randomize a pandas column in the same order every time I run the code in Python?

This is my code:

random_idx = np.random.permutation(len(cIds))
train_Ids = cIds[random_idx[:train_size]]

Now, I want the list to be randomized in the same order every time I run this line of code.

Note: I do not want to save random_idx variable in a text file, and fetch the same list.

Upvotes: 2

Views: 112

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150785

Or you can do in in pandas style:

cIds.sample(n=train_size, 
            replace=False, 
            random_state=your_favorite_number_here)

Upvotes: 1

Vlad Bezden
Vlad Bezden

Reputation: 89685

You can use seed in order to tell numpy to generate the same random numbers:

np.random.seed(seed=1234)
random_idx = np.random.permutation(len(cIds))

same as:

np.random.seed(1234)
random_idx = np.random.permutation(len(cIds))

Or

random_idx = np.random.RandomState(seed=1234).permutation(len(cIds)

seed: Must be convertible to 32 bit unsigned integers`

Upvotes: 6

Related Questions