【发布时间】:2020-11-13 21:10:54
【问题描述】:
我想从一系列列表中的每个列表中随机选择一个元素。
import pandas as pd
import numpy as np
l=[['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l'],['m','n','o']]
s = pd.Series(l)
所以s 是:
0 [a, b, c]
1 [d, e, f]
2 [g, h, i]
3 [j, k, l]
4 [m, n, o]
dtype: object
我知道我可以做到以下几点:
s = pd.Series([np.random.choice(i) for i in s])
哪个有效:
0 a
1 e
2 h
3 j
4 m
dtype: object
但我想知道是否有非循环方法可以做到这一点?
例如,(假设每个list 的大小相同)您可以创建一个随机索引数组来尝试从每个list 中选择不同的元素:
i = np.random.randint(3, size=len(l))
#array([2, 2, 0, 1, 0])
但是说s[i] 是行不通的,因为这是索引s 而不是应用到每个list:
2 [g, h, i]
2 [g, h, i]
0 [a, b, c]
1 [d, e, f]
0 [a, b, c]
dtype: object
我的动机是拥有可以在大量列表上工作的东西,从而避免循环。但如果我的列表理解似乎是最合理的,或者没有内置的 pandas/numpy 函数,请告诉我。
【问题讨论】:
-
我认为这可能是最好的方法 s = pd.Series([np.random.choice(i) for i in s])