Python对数据帧进行采样

时间:2016-06-27 19:34:56

标签: python numpy pandas

我有一个现有的数据框“产品”:

    Nr    Product  Verkoopprijs
0    1  Product A          1111
1    2  Product B          1320
2    3  Product C           727
3    4  Product D           783
4    5  Product E          1431
5    6  Product F           421
6    7  Product G           611
7    8  Product H          1244
8    9  Product I           952
9   10  Product J           856
10  11  Product K           660
11  12  Product L          1202
12  13  Product M           720
13  14  Product N          1046
14  15  Product O           980
15  16  Product P           679
16  17  Product Q          1049
17  18  Product R           874
18  19  Product S           430
19  20  Product T           781
20  21  Product U           772
21  22  Product V           806
22  23  Product W          1286
23  24  Product X           776
24  25  Product Y          1057
25  26  Product Z           545

基于此,我想创建一个10000行的新数据框,每行包含来自'products'的随机选择行。

我使用Pandas和Numpy

1 个答案:

答案 0 :(得分:1)

此答案属于@ user1669710,如果他们选择发布答案,请选择答案。

请务必使用replace=True因为您要求拍摄的内容比抽取的内容更多。

df.sample(10000, replace=True)

用于显示目的。

df.sample(10, replace=True)

    Nr    Product  Verkoopprijs
25  26  Product Z           545
10  11  Product K           660
1    2  Product B          1320
16  17  Product Q          1049
3    4  Product D           783
23  24  Product X           776
0    1  Product A          1111
1    2  Product B          1320
19  20  Product T           781

如果你想要reset_index()

df.sample(10, replace=True).reset_index(drop=True)

   Nr    Product  Verkoopprijs
0   4  Product D           783
1  21  Product U           772
2  24  Product X           776
3  19  Product S           430
4  16  Product P           679
5  19  Product S           430
6  15  Product O           980
7   4  Product D           783
8  12  Product L          1202
9  14  Product N          1046