以下错误是由以下代码引起的.我已经阅读了
KeyError: 0 is due to a dictionary file lacking an entry,但我仍然不知道字典文件是什么或我的代码是如何访问它的:我只是试图访问数据帧中的数据.显然问题是数据框VolValues的一个子集使用从23000左右开始的索引,而我试图用索引’0’对其进行切片,因为我认为这是python的“第一个元素”语法.
你能告诉我代码有什么问题以及如何修复它吗?
runfile(‘/Users/daniel/Documents/programming/RectumD2Metrics.py’, wdir=’/Users/daniel/Documents/programming’)
Traceback (most recent call last):
File “”, line 1, in
runfile(‘/Users/daniel/Documents/programming/RectumD2Metrics.py’, wdir=’/Users/daniel/Documents/programming’)
File “/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, line 705, in runfile
execfile(filename, namespace)
File “/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)
File “/Users/daniel/Documents/programming/RectumD2Metrics.py”, line 37, in
D2Planned = interpD2(‘planned’,df)
File “/Users/daniel/Documents/programming/RectumD2Metrics.py”, line 30, in interpD2
if (VolValues[loop] > 2) and (VolValues[loop+1] < 2):
File “/anaconda3/lib/python3.6/site-packages/pandas/core/series.py”, line 766, in __getitem__
result = self.index.get_value(self, key)
File “/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py”, line 3103, in get_value
tz=getattr(series.dtype, ‘tz’, None))
File “pandas/_libs/index.pyx”, line 106, in pandas._libs.index.IndexEngine.get_value
File “pandas/_libs/index.pyx”, line 114, in pandas._libs.index.IndexEngine.get_value
File “pandas/_libs/index.pyx”, line 162, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/hashtable_class_helper.pxi”, line 958, in pandas._libs.hashtable.Int64HashTable.get_item
File “pandas/_libs/hashtable_class_helper.pxi”, line 964, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0
代码:
# Import Rectum DVH
import pandas
import numpy
df = pandas.read_csv(‘/Users/daniel/Documents/data/DVH/RectumData.csv’,
delimiter=’,’,header=0)
# Calculate D_2%, defined by ICRU 78 as “the greatest dose which all but
# 2 percent of a [volume of interest] receives.” aka D_{near-max}
def interpD2(disttype,df):
# Loop through all patients’ plans.
Dose2Results = numpy.zeros(40)
for num in range(0,40):
# We know a priori that there is no DVH data with Volume = 2. Hence we look for
# the two columns less than and greater than Volume = 2.
if disttype == ‘planned’:
DoseValues = df.loc[(df[‘StudyID’] == num+1) & (df[‘DistributionType’] == ‘planned’)].Dose
VolValues = df.loc[(df[‘StudyID’] == num+1) & (df[‘DistributionType’] == ‘planned’)].Volume
else:
DoseValues = df.loc[(