Skip to content
Advertisement

how to randomize a matrix keeping the rows fixed in python

I’m trying to randomize all the rows of my DataFrame but with no success. What I want to do is from this matrix

JavaScript

to this

JavaScript

I’ve tried with np. random.shuffle but it doesn’t work.

I’m working in Google Colaboratory environment.

Advertisement

Answer

If you want to make this work with np.random.shuffle, then one way would be to extract the rows into an ArrayLike structure, shuffle them in place and then recreate the DataFrame:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement