简体   繁体   English

创建一个列表列表,其中另一列提到了列表的长度

[英]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.

相关问题 创建列表列表,其中每个元素的长度在另一个列表中定义 - Create list of lists, where the length of each element is defined in another list 如何从pandas中的列创建唯一ID列表,其中ID列表在Python中被提及为字符串 - How to create a list of unique ID from a column in pandas where lists of ID are mentioned as strings in Python 列表列表中列的最大长度 - maximum length of a column in a list of lists 创建元组列表的列表,其中每个元组是第一个出现的字母及其列表中的行和列 - Create a list of lists of tuples where each tuple is the first occurrence of a letter along with its row and column in the list of lists 将列表列表与python中的某个长度的另一个列表列表相结合 - Combine list of lists to another list of lists by a certain length in python 在列表列表中查找每列的最大长度 - Find max length of each column in a list of lists 计算列表与列表的熊猫列的交集长度 - Counting length of intersection of a list with pandas column of lists 如何创建列表列表,其中子列表是每列的列值 - How to create a list of lists where the sub-lists are the column values for each column 根据列表列表的长度为字典创建键 - Create keys for dictionary based on length of list of lists 在 python 中创建一个列表列表,其中包含单词的长度和计数 - Create a list of lists in python with length and count for words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM