[英]Selecting data from dataframe randomly but do not REPEAT
If I have a dataframe such as number from 1 to 50, then I need ' X
' sets of number, each set contains ' Y
' random numbers. 如果我有一个数据框,例如从1到50的数字,那么我需要' X
'组数字,每组包含' Y
'个随机数。 And the number could not be REPEAT. 而且号码不能重复。
For example, X =5 and Y=5
例如, X =5 and Y=5
X1 1, 8, 12, 17, 16
X2 9, 22, 45, 47, 23
X3 13, 19, 21, 34, 50
X4 46, 49, 29, 38, 11
X5 33, 26, 14, 7, 6
Does anyone know how could I do this by python Pandas or random package or by R? 有谁知道我怎么能通过python Pandas或random package或R来做到这一点?
In [64]:
np.random.choice(np.arange(1,51), (5,5), replace=False)
Out[64]:
array([[18, 35, 20, 39, 7],
[27, 41, 26, 30, 14],
[47, 23, 17, 40, 38],
[34, 6, 3, 42, 31],
[48, 49, 16, 15, 1]])
You can then pass this to the df constructor: 然后,您可以将其传递给df构造函数:
In [65]:
pd.DataFrame(np.random.choice(np.arange(1,51), (5,5), replace=False))
Out[65]:
0 1 2 3 4
0 35 41 38 50 26
1 24 34 14 2 29
2 8 43 25 17 49
3 30 5 40 6 21
4 20 11 31 33 23
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.