Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

python - 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

question from:https://stackoverflow.com/questions/65926033/randomly-swap-elements-of-two-columns-pandas-dataframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...