我努力了
import numpy as np
import paandas as pd
df1 = pd.read_csv('Trans_ZS_Control_64')
df = df1.apply(np.random.permutation)
这置换了整个数据,但我只希望将最后一列的值置换为最多100次。
我该如何进行。
输入数据
0.051424 0.535067 0.453645 0.161857 -0.017189 -0.001850 0.481861 0.711553 0.083747 0.583215 ... 0.541249 0.048360 0.370659 0.890987 0.723995 -0.014502 1.295998 0.150719 0.885673 1
-0.067129 0.673519 0.212407 0.195590 -0.034868 -0.231418 0.480255 0.643735 -0.054970 0.511684 ... 0.524751 0.206757 0.578314 0.614924 0.230632 -0.074980 0.747007 0.047382 1.413796 1
-0.994564 -0.881392 -1.150127 -0.589125 -0.663275 -0.955622 -1.088923 -1.210452 -0.922861 -0.689851 ... -0.442188 -1.294110 -0.934985 -1.085506 -0.808874 -0.779111 -1.032484 -1.026208 -0.248476 1
-0.856323 -0.619472 -1.113073 -0.691285 -0.515566 -1.080643 -0.513487 -0.912825 -1.010245 -0.870335 ... -0.941149 -1.012917 -1.647812 -0.654150 -0.735166 -0.984510 -0.949168 -1.052115 -0.052492 1
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
-0.145871 0.832727 -0.003379 0.327546 1.409891 0.840316 0.700613 0.184477 0.962488 0.200397 ... -0.337530 0.988197 0.751663 0.480126 0.663302 -0.522189 0.512744 -0.063515 1.125415 0
0.972923 0.857971 -0.195672 0.190443 1.652155 0.763571 0.604728 0.115846 0.942269 0.453387 ... -0.522834 0.985770 0.570573 0.438632 0.737030 -0.445704 0.387023 0.031686 1.266407 0
0.281427 1.060266 0.172624 0.258344 1.544505 0.859777 0.689876 0.439106 0.955198 0.335523 ... -0.442724 0.929343 0.707809 0.290670 0.688595 -0.438848 0.762695 -0.105879 0.944989 0
0.096601 1.112720 0.105861 -0.133927 1.526764 0.773759 0.661673 -0.007070 0.884725 0.478899 ... -0.404426 0.966646 0.994733 0.418965 0.862612 -0.174580 0.407309 -0.010520 1.044876 0
-0.298780 1.036580 0.131270 0.019826 1.381928 0.879310 0.619529 -0.022691 0.982060 -0.039355 ... -0.702316 0.985320 0.457767 0.215949 0.752685 -0.405060 0.166226 -0.216972 1.021018 0
预期的输出:随机排列最后一列
0.051424 0.535067 0.453645 0.161857 -0.017189 -0.001850 0.481861 0.711553 0.083747 0.583215 ... 0.541249 0.048360 0.370659 0.890987 0.723995 -0.014502 1.295998 0.150719 0.885673 0
-0.067129 0.673519 0.212407 0.195590 -0.034868 -0.231418 0.480255 0.643735 -0.054970 0.511684 ... 0.524751 0.206757 0.578314 0.614924 0.230632 -0.074980 0.747007 0.047382 1.413796 0
-0.994564 -0.881392 -1.150127 -0.589125 -0.663275 -0.955622 -1.088923 -1.210452 -0.922861 -0.689851 ... -0.442188 -1.294110 -0.934985 -1.085506 -0.808874 -0.779111 -1.032484 -1.026208 -0.248476 1
-0.856323 -0.619472 -1.113073 -0.691285 -0.515566 -1.080643 -0.513487 -0.912825 -1.010245 -0.870335 ... -0.941149 -1.012917 -1.647812 -0.654150 -0.735166 -0.984510 -0.949168 -1.052115 -0.052492 1
.. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
-0.145871 0.832727 -0.003379 0.327546 1.409891 0.840316 0.700613 0.184477 0.962488 0.200397 ... -0.337530 0.988197 0.751663 0.480126 0.663302 -0.522189 0.512744 -0.063515 1.125415 0
0.972923 0.857971 -0.195672 0.190443 1.652155 0.763571 0.604728 0.115846 0.942269 0.453387 ... -0.522834 0.985770 0.570573 0.438632 0.737030 -0.445704 0.387023 0.031686 1.266407 1
0.281427 1.060266 0.172624 0.258344 1.544505 0.859777 0.689876 0.439106 0.955198 0.335523 ... -0.442724 0.929343 0.707809 0.290670 0.688595 -0.438848 0.762695 -0.105879 0.944989 0
0.096601 1.112720 0.105861 -0.133927 1.526764 0.773759 0.661673 -0.007070 0.884725 0.478899 ... -0.404426 0.966646 0.994733 0.418965 0.862612 -0.174580 0.407309 -0.010520 1.044876 0
-0.298780 1.036580 0.131270 0.019826 1.381928 0.879310 0.619529 -0.022691 0.982060 -0.039355 ... -0.702316 0.985320 0.457767 0.215949 0.752685 -0.405060 0.166226 -0.216972 1.021018 1
最佳答案
不确定这是否是您的意思,但是您可以这样做
import pandas as pd
import numpy as np
v = np.arange(0,10)
df = pd.DataFrame({'c1': v, 'c2': v, 'c3': v})
df
这将创建以下df:
c1 c2 c3
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
要置换最后一列,您可以运行以下命令:
df1 = df.copy()
df1.c3 = np.random.permutation(df1.c3)
df1
导致:
c1 c2 c3
0 0 0 5
1 1 1 9
2 2 2 2
3 3 3 6
4 4 4 0
5 5 5 4
6 6 6 8
7 7 7 7
8 8 8 1
9 9 9 3
希望对您有所帮助
关于python - 仅使用python置换最后一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60637716/