python - How to shuffle a pandas dataframe according to a categorical column -


say have dataframe can grouped person column (a, b, c, d, etc.), , each of people has number of records. question how can shuffle people without changing order within each group?

consider dataframe df

df = pd.dataframe(dict(     person=list('bbbeeeaaaccczzzzz'),     other=range(17) ))      other person 0       0      b 1       1      b 2       2      b 3       3      e 4       4      e 5       5      e 6       6      7       7      8       8      9       9      c 10     10      c 11     11      c 12     12      z 13     13      z 14     14      z 15     15      z 16     16      z 

use np.random.permutation reshuffle person , set_index/loc/reset_index

df.set_index('person').loc[     np.random.permutation(df.person.unique()) ].reset_index()     person  other 0       z     12 1       z     13 2       z     14 3       z     15 4       z     16 5            6 6            7 7            8 8       c      9 9       c     10 10      c     11 11      b      0 12      b      1 13      b      2 14      e      3 15      e      4 16      e      5 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -