randomly swap elements of two columns pandas dataframe

Viewed 143

how to randomly swap elements of two columns pandas DataFrame with given probability? Doing this takes very long:

import random

for i in range(len(df)):
 if random.random() < 0.5:
  df.loc[i, 'A'], df.loc[i, 'B'] = df['B'].loc[i], df['A'].loc[i]

df

   A   B
0  9   14      
1  1   32     
2  8   23 
3  1    2    
4  10   66 

There is a similar question here

1 Answers

Try:

# sample data
df = pd.DataFrame(np.arange(20).reshape(10,2), columns=['A','B'])

# random indexing, seed for repeatability
# remove seed for randomness
np.random.seed(42)
idx = np.random.rand(len(df)) < 0.5

# passing numpy array to bypass column alignment
df.loc[idx, ['A','B']] = df.loc[idx, ['B','A']].to_numpy()

Output:

    A   B
0   1   0
1   2   3
2   4   5
3   6   7
4   9   8
5  11  10
6  13  12
7  14  15
8  16  17
9  18  19
Related