KeyError:1个python中的代码-无法找出问题 [英] KeyError: 1 Code in python - can't figure out the issue
问题描述
此代码曾经有效,现在不再有效.对于我的一生,无法弄清楚哪里出了问题.我猜它与for循环有关,仅此而已.
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.
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
谢谢.
这是我得到的错误代码...
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
推荐答案
您已经如下定义了n
:
n = 15
然后,您定义一行数据框:
You then define a dataframe with one row:
print(df)
Year Reserves Prod
0 2017 442.364 27.8
然后您遍历range(1, 16)
:
for i in range(1, n+1):
FY = df['Year'][i-1] + 1
但是,由于您的数据框只有一行索引为0
的行,因此df['Year'][1]
将以KeyError
返回.这正是您应该期望的.
However, since your dataframe has only one row with index 0
, df['Year'][1]
will return with KeyError
. This is exactly what you should expect.
您尚未说明要达到的目标.您可能希望在一个新问题中这样做.上面的说明描述了为什么获得错误.通常,print
是确定每个阶段发生情况的有用功能.
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.
这篇关于KeyError:1个python中的代码-无法找出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!