Python 数据帧中的多列混洗
我有这样一个数据框:Python 数据帧中的多列混洗,python,pandas,numpy,scikit-learn,sklearn-pandas,Python,Pandas,Numpy,Scikit Learn,Sklearn Pandas,我有这样一个数据框: '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' 'd'
'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' 'd' 'e' 'f'
'hello3.text' 5 8 'hello2.text' 2 10
'hello.text' 1 2 'hello4.text' 8 15
现在我需要将“a”、“b”、“c”列随机排列在一起。
像这样的事情:
'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' 'd' 'e' 'f'
'hello3.text' 5 8 'hello2.text' 2 10
'hello.text' 1 2 'hello4.text' 8 15
如何执行此操作?使用
np.random.permutation
分别处理每列,因为不同类型的数据:
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
将
'a',b',c'
列随机化在一起意味着只对这些特定列的行洗牌?如果是,则以下内容满足您的需要:
cols=['a','b','c']
df[cols]=df[cols]。样本(分形=1.0,随机状态=0)。重置索引(drop=True)
打印(df)
a b c d e f
0 hello3.txt 5 8 hello2.text 2 10
1你好。文字1你好。文字8 15
您可以使用random\u state
参数控制随机化