0

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
Mohamed Amine
  • 340
  • 1
  • 4
  • 16

1 Answers1

1

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
not_speshal
  • 22,093
  • 2
  • 15
  • 30