Home > front end >  how to do permuation between values of 2 columns in pandas
how to do permuation between values of 2 columns in pandas

Time:05-02

let s say I have a dataframe containing tennis matches each match contains the name of the player 1 and the player 2

di={'player1':["adam","luke","roy"], 'player2': ["james","steve","derek"]}
df=pd.DataFrame(data=di)
df

so I have something like this

player1   player2
0    adam   james
1    luke   steve
2     roy   derek

I want to Randomly do a permutation between the 2 columns which something completely fine because the columns describe the same entity in the same context and have something like this

  player1 player2
0   james   adam 
1   steve    luke
2     roy   derek

CodePudding user response:

Try with sample. Since sample works on rows, you need to do a double transpose to shuffle columns:

>>> df.T.sample(frac=1).T

  player2 player1
0   james    adam
1   steve    luke
2   derek     roy
  • Related