问题描述
我是numpy和pandas的新手,我正在尝试编写这段代码来创建pandas系列。对于该系列中的每个索引,我想从上面的列表中随机选择一个随机数的兴趣点,在这种情况下为1-3,没有重复项。如果可能的话,我想找到改善代码的方法。
谢谢
def random_interests(num):
interests = [1,2,3,4,5,6]
stu_interests = []
for n in range(num):
stu_interests.append(np.random.choice(interests,np.random.randint(1,4),replace=False))
rand_interests = pd.Series(stu_interests)
解决方法
您必须在函数中添加一个return,以便从中获得结果。通过在底部添加一个返回,您的代码将是:
def random_interests(num):
interests = [1,2,3,4,5,6]
stu_interests = []
for n in range(num):
stu_interests.append(np.random.choice(interests,np.random.randint(1,4),replace=False))
rand_interests = pd.Series(stu_interests)
return rand_interests
通过运行n = 5输出为:
random_interests(5)
0 [5,6,4]
1 [5]
2 [1,4]
3 [3,1]
4 [1,2]
dtype: object
,
这是一种实现方法。通过使用更快的Apply可以避免for循环。另外,您无需创建单独的变量stu_interests
,而在num
高的情况下会消耗更多的内存
def random_interests(num):
interests = [1,6]
rand_interests = pd.DataFrame(np.nan,index=[x for x in range(num)],columns=['random_list'])
rand_interests['random_list'] = rand_interests['random_list'].apply(lambda x: np.random.choice(interests,replace=False))
return rand_interests['random_list']
,
或者一个衬里
pd.Series([np.random.choice(
[1,6],replace=False)
for i in range(num)])
输出:
0 [3,6]
1 [4,1]
2 [6,5]
3 [3]