[英]KeyError: 1 Code in python - can't figure out the issue
This code used to work and no longer does. 此代码曾经可以使用,现在不再可用。 For the life of me can't figure out what is wrong.
对于我的生活,我无法弄清楚哪里出了问题。 I am guessing it has to do with the for loop but that is about it.
我猜想它与for循环有关,仅此而已。
import pandas as pd
LastFY = 2017
n = 15
DeclRate = 0.1
Res = 442.364
LFY_Vol = 27.8
Col_Names = ['Year', 'Reserves', 'Prod']
df = pd.DataFrame(columns=Col_Names)
df_add = pd.DataFrame(columns=Col_Names)
df['Year'] = [LastFY]
df['Prod'] = [LFY_Vol]
df['Reserves'] = [Res]
for i in range(1, n+1):
FY = df['Year'][i-1] + 1
if df['Prod'][i-1] * (1 - DeclRate) > df['Reserves'][i-1]:
Prod_A = df['Reserves'][i-1]
else:
Prod_A = df['Prod'][i-1] * (1 - DeclRate) if i < n else df['Reserves'][i-1]
Reserves_A = df['Reserves'][i-1] - Prod_A
df_add['Year'] = [FY]
df_add['Reserves'] = [Reserves_A]
df_add['Prod'] = [Prod_A]
df = df.append(df_add, ignore_index=True)
df.round(0).style
Thank you. 谢谢。
Here is the error code I get... 这是我得到的错误代码...
runfile('C:/Users.../TEST.py', wdir='C:/Users...')
Traceback (most recent call last):
File "<ipython-input-175-365e0e41c561>", line 1, in <module>
runfile('C:/Users.../TEST.py', wdir='C:/Users...')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users.../TEST.py", line 22, in <module>
FY = df['Year'][i-1] + 1
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 623, in __getitem__
result = self.index.get_value(self, key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2560, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas/_libs/index.pyx", line 83, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 91, in pandas._libs.index.IndexEngine.get_value
File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 811, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 817, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: -1
You have defined n
as follows: 您已将
n
定义如下:
n = 15
You then define a dataframe with one row: 然后,您定义一行数据框:
print(df)
Year Reserves Prod
0 2017 442.364 27.8
You then iterate over range(1, 16)
: 然后,您遍历
range(1, 16)
1,16 range(1, 16)
:
for i in range(1, n+1):
FY = df['Year'][i-1] + 1
However, since your dataframe has only one row with index 0
, df['Year'][1]
will return with KeyError
. 但是,由于您的数据框只有一行索引为
0
行,因此df['Year'][1]
将返回KeyError
。 This is exactly what you should expect. 这正是您应该期望的。
You haven't explained what you are trying to achieve. 您尚未说明要达到的目标。 You may wish to do so in a new question .
您可能希望在新问题中这样做。 The above explanation describes why you have obtained your error.
上面的说明描述了为什么获得错误。 In general,
print
is a useful function to determine what is happening at each stage. 通常,
print
是确定每个阶段发生情况的有用功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.