How to randomly shuffle a populaiton by preserving all properites except one?

后端 未结 2 1701
孤独总比滥情好
孤独总比滥情好 2021-01-28 14:17

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,

相关标签:
2条回答
  • 2021-01-28 14:45

    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.

    0 讨论(0)
  • 2021-01-28 14:50

    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.

    0 讨论(0)
提交回复
热议问题