Skip to content Skip to sidebar Skip to footer

How To Randomly Shuffle A Populaiton By Preserving All Properites Except One?

A spherical region of space is filled with a specific distribution of smaller, different size spheres. Each sphere is associated with some physical properties: position, radius, ma

Solution 1:

If you're using pandas, you could just shuffle one column:

df['col'] = df['col'].sample(frac=1).values

This works equally well on any subset of columns, e.g.

cols = ['col1', 'col2']
df[cols] = df[cols].sample(frac=1).values

The two columns are shuffled together, i.e. their respective values remain aligned.

See also this answer.

Solution 2:

You can implement a Knuth shuffle (https://en.wikipedia.org/wiki/Random_permutation), its quite straight-forward.

You can adapt the implementation algorithm to only swap your desired properties.

Post a Comment for "How To Randomly Shuffle A Populaiton By Preserving All Properites Except One?"