I’m trying to randomize all the rows of my DataFrame but with no success. What I want to do is from this matrix
A= [ 1 2 3 4 5 6 7 8 9 ]
to this
A_random=[ 4 5 6 7 8 9 1 2 3 ]
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
:
A = pandas.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) extracted_rows = A.values.tolist() # Each row is an array element, so rows will remain fixed but their order shuffled np.random.shuffle(extracted_rows) A_random = pandas.DataFrame(extracted_rows)