gpt4 book ai didi

python - 随机选择元组中的值

转载 作者:行者123 更新时间:2023-12-01 00:25:41 25 4
gpt4 key购买 nike

有没有办法使用“records_to_select”列中的值作为“k”,以便从“pnrTuple”列中选择随机值的数量(如下所示(35784905, 40666303, 47603805, 68229102 ) - 一个元组)。我需要对整个数据框执行此操作。基本上,如果这是 'pnrTuple' (35784905, 40666303, 47603805, 68229102) 的值,并且这个 3 是 'records_to_select' 的值,我需要随机选择 3 id 的。我愿意接受任何其他方式来做到这一点。代码显然不起作用,我只是想展示我想做的事情。

mass_grouped3['pnrTuple'] = mass_grouped3['pnrTuple'].map(lambda x: random.choices(x, k=mass_grouped3['records_to_select']))



bula gender age hhgr pnr freq records_to_select pnrTuple
1 1 1 3 ['35784905', '40666303', '47603805', '68229102'] 4 4 ('35784905', '40666303', '47603805', '68229102')
1 1 2 1 ['06299501', '07694901', '35070201', '36765601', '97818801'] 5 5 ('06299501', '07694901', '35070201', '36765601', '97818801')
1 1 2 2 ['17182402'] 1 1 ('17182402',)
1 1 2 3 ['07992601', '20164401', '26817203', '50584001'] 4 4 ('07992601', '20164401', '26817203', '50584001')
1 1 3 1 ['07935501', '08720401', '19604501', '26873301', '46069001', '65829601'] 6 6 ('07935501', '08720401', '19604501', '26873301', '46069001', '65829601')
1 1 3 2 ['06529901', '21623801', '21624202', '31730001', '35448801', '36460001', '79142201', '98476701'] 8 5 ('06529901', '21623801', '21624202', '31730001', '35448801', '36460001', '79142201', '98476701')
1 1 3 3 ['08786301', '17729602', '34827202', '35191802', '36106801', '41139001', '60815801', '65889401', '82642901', '89476501', '97523201', '98668501'] 12 8 ('08786301', '17729602', '34827202', '35191802', '36106801', '41139001', '60815801', '65889401', '82642901', '89476501', '97523201', '98668501')
1 1 4 1 ['04282501', '07389801', '08988001', '13514901', '33755101', '36010101', '40009501', '46641001', '49795401', '51045401', '78502101', '84993601', '85047501'] 13 9 ('04282501', '07389801', '08988001', '13514901', '33755101', '36010101', '40009501', '46641001', '49795401', '51045401', '78502101', '84993601', '85047501')
1 1 4 2 ['05250501', '17896401', '27035401', '32701701', '34741602', '42196402', '42891001', '67090301', '69240301', '77546701', '87855401', '96712602'] 12 8 ('05250501', '17896401', '27035401', '32701701', '34741602', '42196402', '42891001', '67090301', '69240301', '77546701', '87855401', '96712602')
1 1 4 3 ['08047701', '08735402', '15113502', '16648302', '21618901', '26166801', '36508001', '40297801', '42864202', '47068001', '54051002', '68229104', '68555401', '76081901', '80639302', '86100502', '88471102', '98655102', '98672301'] 19 13 ('08047701', '08735402', '15113502', '16648302', '21618901', '26166801', '36508001', '40297801', '42864202', '47068001', '54051002', '68229104', '68555401', '76081901', '80639302', '86100502', '88471102', '98655102', '98672301')
1 1 5 1 ['06027001', '14817601', '17035701', '26482001', '40580701', '41411301', '43383101', '50290201', '66963901', '98378101'] 10 7 ('06027001', '14817601', '17035701', '26482001', '40580701', '41411301', '43383101', '50290201', '66963901', '98378101')
1 1 5 2 ['04215802', '04986702', '06021301', '07696001', '08310701', '09248301', '10429402', '13377101', '14652801', '14742402', '16179901', '19003801', '26296401', '30262201', '32109302', '42196401', '43343005', '69230101', '79169901', '81551801', '85026001', '88785201'] 22 15 ('04215802', '04986702', '06021301', '07696001', '08310701', '09248301', '10429402', '13377101', '14652801', '14742402', '16179901', '19003801', '26296401', '30262201', '32109302', '42196401', '43343005', '69230101', '79169901', '81551801', '85026001', '88785201')
1 1 5 3 ['06208701', '10235601', '11200904', '26165901', '28133401', '30318101', '42304401', '48289402', '68324402', '79444601', '86214301', '89292601', '89644901', '95844702', '98833201'] 15 10 ('06208701', '10235601', '11200904', '26165901', '28133401', '30318101', '42304401', '48289402', '68324402', '79444601', '86214301', '89292601', '89644901', '95844702', '98833201')
1 1 6 1 ['04076601', '04299501', '05992601', '06070001', '06749701', '10940601', '11880801', '13789901', '15641601', '15652201', '16359701', '17115201', '17944501', '27168601', '30034901', '40494901', '41876001', '43269501', '43443801', '65935901', '72038401', '76173101', '85624501', '85865301', '86858901', '88302301', '97266501'] 27 19 ('04076601', '04299501', '05992601', '06070001', '06749701', '10940601', '11880801', '13789901', '15641601', '15652201', '16359701', '17115201', '17944501', '27168601', '30034901', '40494901', '41876001', '43269501', '43443801', '65935901', '72038401', '76173101', '85624501', '85865301', '86858901', '88302301', '97266501')
1 1 6 2 ['00305501', '00364401', '00467701', '06004101', '06760101', '13484301', '14101401', '14604101', '15296601', '16701801', '17295801', '19292501', '21692601', '22043401', '26117302', '30296102', '31566301', '32082501', '32975801', '33007502', '33901301', '36627901', '40933601', '40950801', '40953901', '41599201', '41647601', '42030702', '43249601', '43253601', '46177002', '46425001', '60285901', '62801802', '63203001', '63641601', '71358803', '72198201', '78789501', '79287901', '82297701', '85000802', '85458401', '86637402', '86755601', '87113101', '87312501', '87457701', '87617901', '96706301', '97494201', '97549601'] 52 36 ('00305501', '00364401', '00467701', '06004101', '06760101', '13484301', '14101401', '14604101', '15296601', '16701801', '17295801', '19292501', '21692601', '22043401', '26117302', '30296102', '31566301', '32082501', '32975801', '33007502', '33901301', '36627901', '40933601', '40950801', '40953901', '41599201', '41647601', '42030702', '43249601', '43253601', '46177002', '46425001', '60285901', '62801802', '63203001', '63641601', '71358803', '72198201', '78789501', '79287901', '82297701', '85000802', '85458401', '86637402', '86755601', '87113101', '87312501', '87457701', '87617901', '96706301', '97494201', '97549601')
1 1 6 3 ['10368305', '17205801', '20164403', '26295901', '26817201', '40666302', '60751201', '89908101'] 8 5 ('10368305', '17205801', '20164403', '26295901', '26817201', '40666302', '60751201', '89908101')
1 2 1 1 ['00854101'] 1 1 ('00854101',)

最佳答案

让我们首先创建一个虚拟数据框:

import random
df = pd.DataFrame({'id':range(1, 101),
'tups':[(random.randint(1, 1000000), random.randint(1, 1000000), random.randint(1, 1000000),
random.randint(1, 1000000), random.randint(1, 1000000), random.randint(1, 1000000)) for _ in range(100)],
'records_to_select':[random.randint(1, 5) for _ in range(100)]})

让我们看一下该数据框:

df.head()

id tups records_to_select
0 1 (59216, 532002, 799100, 829539, 968212, 62046) 5
1 2 (217750, 448108, 333314, 417604, 330570, 991236) 2
2 3 (352810, 242235, 466270, 169478, 155754, 29238) 3
3 4 (309312, 867221, 304830, 278511, 547559, 72195) 1
4 5 (872128, 556190, 112937, 33984, 759746, 549025) 2

这里我们有一个 tups 列,我们要从中随机选择,然后是一个 records_to_select 列,我们要在其中采样一个数字。

我通常解决问题的方法是先做一次。这里我们只创建一个元组,然后弄清楚如何从中随机采样。一种方法是random.sample(list, number)。我让它也返回一个元组,尽管它默认返回一个列表:

x = (3, 7, 5, 0, 2, 8, 6, 1)
tuple(random.sample(x, 4))
(3, 0, 5, 8)

现在我们可以使用 applylambda 来迭代数据帧并将其应用到每一行。

df['samples_from_tuple'] = df.apply(lambda x: tuple(random.sample(x['tups'], x['records_to_select'])), axis=1)


df.head()

id tups records_to_select samples_from_tuple
0 1 (476833, 384041, 789847, 233342, 527508, 892565) 4 (384041, 233342, 527508, 476833)
1 2 (759298, 654362, 244128, 851410, 233892, 612689) 2 (759298, 851410)
2 3 (640435, 391573, 290131, 277103, 250173, 756359) 2 (391573, 277103)
3 4 (788502, 128537, 560571, 42867, 47120, 71505) 1 (47120,)
4 5 (356955, 813874, 731805, 943841, 972449, 247512) 5 (356955, 972449, 813874, 731805, 247512)

关于python - 随机选择元组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58597920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com