- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法使用“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)
现在我们可以使用 apply
和 lambda
来迭代数据帧并将其应用到每一行。
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/
typing模块中使用List、Tuple等有什么区别: from typing import Tuple def f(points: Tuple): return map(do_stuff,
如何遍历列表的每 5 个元素并将它们组成一个元组,然后将同一列表的第 6 个元素作为第二个元组 - 然后对接下来的 5 个元素和第 6 个元素执行相同的操作。 我读过 operator.itemget
我有一个 Seq[((元组 A),(元组 B))] 有没有一种简单的方法来对元组 A 进行分组,以便我得到 Seq[(Tuple A, Seq[Tuple B])] 我试过 groupby(x =>
如果我有以下内容 val A = List(1,2,3) val B = List(1,2,3) 这两个变量是否有相同的内存地址? 最佳答案 它们不会有相同的内存地址,可以使用 eq 方法确认,com
我实际上是在尝试创建一个配对列表,但事实证明这非常困难 在有人提到 Hashtables 之前请注意,会有我不关心的重复项。 例如,如果我这样做 $b = @{"dog" = "cat"} 我明白了
我正在尝试为其他资源中的 for_each 循环创建局部变量,但无法按预期制作局部映射。 以下是我试过的。 (地形 0.12) 预期映射到循环 temple_list = { "test2-role"
我目前正在学习 Haskell,在 FP 方面我绝对是初学者。 现在我正在尝试使用列表推导式进行不同的操作。 listComprehension = [(a,b,c) | a <- xs, b <
我正在尝试为其他资源中的 for_each 循环创建局部变量,但无法按预期制作局部映射。 以下是我试过的。 (地形 0.12) 预期映射到循环 temple_list = { "test2-role"
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
如何通过元组中的第三项过滤此类型的列表: type Car = (String, [String], Int [String]) 我看到了 snd和 fst方法,但在这里我认为这行不通,我不确定如何在
有没有办法创建 Tuple 在 Java 中,无需创建多个类? 例如,可以为每种不同类型的元组创建不同的类,每个类具有不同数量的 Type Parameters : public class Sing
我必须处理一堆二维点类型:pair , pair , pair ,并且只要存在坐标转换,我就允许点之间的隐式转换。像这样: template inline operator pair ( pair t
这个问题在这里已经有了答案: How do I iterate through two lists in parallel? (8 个答案) How do I iterate over the tu
编写一个函数 square_odd_terms 接受一个元组作为参数并返回一个元组中奇数项的平方的元组。即使是条款也将保持不变。 我的尝试是: def square_odd_termms(tpl):
更新: 我选择了这个: set(item[1] for item in id) 谢谢你们,你们的想法对我有帮助。 我正在处理一个元组列表: 以下面这行代码为例。我的 list 可以是任何长度。但是,我
我一直在尝试执行此任务,在尝试时我不禁想到会有比我一直尝试的方式更好的编码方式。 我有一行文字和一个关键字。我想在每个列表中的每个字符下创建一个新列表。关键字将重复自身直到列表末尾。如果有任何非字母字
我现在这个问题已经被问过好几次了。但是,答案似乎并没有解决我的问题。我收到类型错误,“元组”对象不可调用。即使列表中的元组以正确的方式用逗号分隔,我也得到了这个: def aiMove(b):
嘿,所以我花了两个多小时试图解决这个问题,但我就是做不对。我猜我犯了一个非常简单的错误,所以如果有人能指出我正确的方向,我将非常感激,谢谢!顺便说一句,这是一门树屋类(class)。 “目前我们的问候
这不是一个严格的编程问题,但为什么是tuple在单独的 header 中定义,而不是添加到 连同 pair ?它看起来更自然,不那么困惑等。 最佳答案 在具有细粒度的 header 和只有一个 hea
我是一名优秀的程序员,十分优秀!