2

I have two dataframes, A and B , with dimension MxN which rows I want to random shuffle. A and B have the same column names and indexes. I know how to shuffle data inside each column with df.apply(np.random.shuffle) method, but it permutate differently each column. I want that if the first row of A becames the second row after shuffle, the first row of B becames the second row too, etc. How can I do what I want?

Vasiliy
  • 61
  • 8
  • I think this is a duplicate of https://stackoverflow.com/questions/29576430/shuffle-dataframe-rows#34879805 – jdowner Jul 25 '18 at 09:42

2 Answers2

2

I was blind. It is simple. If we have a list of permutations PMT. We can do this:

A_new = A.iloc[PMT]
B_new = B.iloc[PMT]

That's all.

Areza
  • 5,623
  • 7
  • 48
  • 79
Vasiliy
  • 61
  • 8
0

This doesn't use pandas but works

from random import shuffle
ind_list=[i for i in range(M)]
shuffle(ind_list)
A=A[ind_list,:]
B=B[ind_list,:]
fireball.1
  • 1,413
  • 2
  • 17
  • 42