|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大佬好,尝试做了一个抽奖程序(代码写的很丑):
#从Excel导入名单
#然后从姓名列进行抽取,一共抽取3次
#每次抽中的人从奖池中移除
import numpy as np
import pandas as pd
import random
f=r"C:\Users\Administrator\Desktop\抽奖名单.xlsx"
df1 = pd.read_excel(f,sheet_name='名单1')
md1 = df1['姓名'][1:]
cj1 = random.choice(md1)
print(cj1)
md2 = md1[~md1.isin([cj1])]
cj2 = random.choice(md2)
print(cj2)
md3 = md2[~md2.isin([cj2])]
cj3 = random.choice(md3)
print(cj3)
大概率可以运行成功,但是有时候会报错,想请教一下各位大佬,是什么原因导致的。
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 61
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\random.py", line 17, in <module>
cj2 = random.choice(md2)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\random.py", line 291, in choice
return seq[i]
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 959, in __getitem__
return self._get_value(key)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 1070, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc
raise KeyError(key) from err
KeyError: 61
这里有个帖子,你可以看一下链接在下面。https://zhidao.baidu.com/question/429201430267134492.html
如果你觉得对你有帮助,请给我最佳答案,谢谢亲。
|
|