rondom函数 报错
各位大佬好,尝试做了一个抽奖程序(代码写的很丑):#从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['姓名']
cj1 = random.choice(md1)
print(cj1)
md2 = md1[~md1.isin()]
cj2 = random.choice(md2)
print(cj2)
md3 = md2[~md2.isin()]
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
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 eee...{:10_324:} 这里有个帖子,你可以看一下链接在下面。https://zhidao.baidu.com/question/429201430267134492.html
如果你觉得对你有帮助,请给我最佳答案,谢谢亲。 keyerror一般是你使用字典里不存在的key产生的错误,避免产生错误的方法很简单,使用字典的get方法,它接受一个key和一个默认值,这个默认值只有key不存在的使用返回,存在则只接访问key的值
a={'a':'b','123':345}
try: 字典,我能不能理解一下,就是我的原表格数据。 shiyouroc 发表于 2022-2-9 17:59
keyerror一般是你使用字典里不存在的key产生的错误,避免产生错误的方法很简单,使用字典的get方法,它接受 ...
就是我的随机抽取,抽到了错误值,所以run不下去了,是这么个意思吧
页:
[1]