Home > OS >  How to shuffle ndarray/pandas dataframe rows within selected columns indices preserving other column
How to shuffle ndarray/pandas dataframe rows within selected columns indices preserving other column

Time:12-21

I have an array:

a = np.array([[1,3,9,8,4],
              [2,4,6,1,9],
              [7,3,0,5,8],
              [1,6,9,2,4]])

I want to randomly select 3 columns and randomly shuffle the elements across those columns (preserving them in the same row).

E.g. if the column_indices = [0,2,3] the new array will be:

a = np.array([[8,3,1,9,4],
              [1,4,6,2,9],
              [0,3,5,7,8],
              [1,6,2,9,4]])

How can I do it? Thanks

CodePudding user response:

You can use numpy.random.shuffle on the slice (shuffle acts on the first axis, which is what you want):

# shuffle in place
np.random.shuffle(a[:, [0,2,3]])

# see changes
print(a)

output:

[[1 3 9 8 4]
 [2 4 6 1 9]
 [7 3 0 5 8]
 [1 6 9 2 4]]

CodePudding user response:

The complete code to use:

column_indices = [0,2,3]

a = np.array([[1,3,9,8,4],
              [2,4,6,1,9],
              [7,3,0,5,8],
              [1,6,9,2,4]])

# transpose to switch from columns to rows
shuffled = a[:, column_indices].transpose()
np.random.shuffle(shuffled)

a[:, column_indices] = shuffled.transpose()
a
  • Related