我有一个这样的数据框:
'a' 'b' 'c' 'd' 'e' 'f'
'hello.text' 1 2 'hello2.text' 2 10
'hello3.text' 5 8 'hello4.text' 8 15
现在我需要随机排列“a”、“b”、“c”列。 像这样的事情:
'a' 'b' 'c' 'd' 'e' 'f'
'hello3.text' 5 8 'hello2.text' 2 10
'hello.text' 1 2 'hello4.text' 8 15
我该怎么做?
最佳答案
使用 np.random.permutation
和 DataFrame.apply
用于单独处理每一列,因为不同类型的数据:
cols = ['a','b','c']
df[cols] = df[cols].apply(lambda x: np.random.permutation(x))
print (df)
a b c d e f
0 'hello.text' 5 2 'hello2.text' 2 10
1 'hello3.text' 1 8 'hello4.text' 8 15
关于python - 在数据框中混洗多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553046/