Python对datafram进行采样

2024-10-03 17:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    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”中随机选择的行。你知道吗

我用熊猫和裸体


Tags: 数据产品productnrproducts裸体verkoopprijs
1条回答
网友
1楼 · 发布于 2024-10-03 17:23:07

此答案属于@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

相关问题 更多 >