简体   繁体   中英

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. And the number could not be REPEAT.

For example, 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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM