简体   繁体   中英

randomly swap elements of two columns pandas dataframe

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM