1

Given a DataFrame like this:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': np.ones(5), 'B': np.zeros(5), 'C': np.ones(5), 'D': np.zeros(5)})

I want to be able to randomly select a number of rows where the A and B value along with their corresponding C and D values - BUT NOT column E and F are - switched so that the B column value is put in the A column and vice versa.

So it's not the whole row, but only certain columns that should switch (A, B, C , D) while E and F keep their values.

Does anybody have any ideas on how to accomplish this?

dfunch
  • 13
  • 4
  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jun 09 '18 at 09:08
  • @jezrael apparently my attempt was what the OP was asking so now you can work your magic :) – roganjosh Jun 09 '18 at 09:32
  • @roganjosh - answer is nice, +1 – jezrael Jun 09 '18 at 09:35
  • @jezrael gosh, I'm actually quite surprised by that. I had assumed that there was some hidden method I was not aware of. Thanks :) – roganjosh Jun 09 '18 at 09:37
  • No, for swap rows like OP need here pandas have no method :) – jezrael Jun 09 '18 at 09:39

1 Answers1

3

I think there definitely could be a more efficient way than taking copies of the Series here:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': np.ones(5), 'B': np.zeros(5), 'C': np.ones(5), 'D': np.zeros(5)})

rows_to_swap = np.random.choice(len(df), size=3, replace=False)

a_column = df['A'].copy()
b_column = df['B'].copy()

df.loc[rows_to_swap, 'A'] = b_column[rows_to_swap]
df.loc[rows_to_swap, 'B'] = a_column[rows_to_swap]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • Thanks for the input! I think you got the random selection and switching of A and B correct, but I would also like that the C and D columns make the same switch, while E and F columns remain the same. More specifically, C should follow A, and D should follow B. I guess it could be a matter of simply including C and D in the a_column/b_column variables, but I'm getting some KeyErrors. I'll try to tinker a bit with it! – dfunch Jun 09 '18 at 09:30
  • @dfunch yes, with my approach it would just be extending to columns C and D because the rows chosen are fixed. – roganjosh Jun 09 '18 at 09:31