[英]create a list of lists where the length of the list is mentioned in another column
My df looks like this; 我的df看起来像这样;
data = pd.DataFrame({'count':[1,2,4,5,8,1,3]})
How do I create a new column called 'lists' which holds a random list where the length of the list is the value in the 'count' column? 如何创建一个名为“列表”的新列,该列包含一个随机列表,其中列表的长度是“计数”列中的值? Something like this... 像这样
data = pd.DataFrame({'count':[1,2,4,5,8,1,3],
'lists': [[1],[34,56],[25,65,1,4],[33,37,89,12,11],[45,56,78,98,12,2,3,4],[234],[23,456,765]]
You could try: 您可以尝试:
data['list'] = data.applymap(lambda x: random.sample(xrange(100), x))
Alternatively, 或者,
data['list'] = data['count'].apply(lambda x: pd.np.random.random_integers(1, 100, x) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.