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