随机交换两列熊猫数据框的元素

时间:2021-01-27 19:27:50

标签: python pandas numpy

如何以给定的概率随机交换两列pandas DataFrame的元素?这样做需要很长时间:

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 

有一个类似的问题here

1 个答案:

答案 0 :(得分:2)

试试:

# 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()

输出:

    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