ReEiving KeyError: 0 при попытке проверить, меньше ли значение в моем столбце данных - PullRequest
1 голос
/ 09 октября 2019

Я довольно новичок в программировании и играю с этим скриптом. Всякий раз, когда я пытаюсь запустить его, я получаю сообщение об ошибке в первом операторе if после цикла for.

Не совсем уверен, что это за ошибка, он говорит, что это KeyError из-за присутствующего нуля.

Что я пытаюсь сделать, это проверить, является ли столбец "Valid"у моего информационного кадра есть значение ниже 0.

Любая помощь будет оценена, полностью потеряна на этом.

Scatter_Volume = pd.DataFrame(columns=["X","Price","BarPrice","Color"])
index = list(range(len(X)))

X = XBTUSD.close
O = XBTUSD.open
V = XBTUSD.volume

Volume_df["VMA"] = V.rolling(20).mean()
Volume_df["Valid"] = Volume_df["VMA"]-V

Volume_step = (max(V)-min(V))/100
Volume_df["Percentage"] = (V/Volume_step)*1.5

for x in range(len(Volume_df)):
    if(Volume_df["Valid"][x]<0):
        if(X[x]>O[x]):
            Scatter_Volume = Scatter_Volume.append({"X":int(x),
                                                   "Price":X[x],
                                                   "BarPrice":Volume_df["VMA"][x],
                                                   "Color": "green"},
                                                  ignore_index=True)
        else:
            Scatter_Volume = Scatter_Volume.append({"X": int(x),
                                                    "Price": X[x],
                                                    "BarPrice": Volume_df["VMA"][x],
                                                    "Color":"red"},
                                                    ignore_index=True)

plt.scatter(Scatter_Volume["X"], Scatter_Volume["BarPrice"], c=Scatter_Volume["Color"])
plt.scatter(Scatter_Volume["X"], Scatter_Volume["BarPrice"], c=Scatter_Volume["Color"], s=Scatter_Volume["Percentage"])
plt.show()
plt.bar(index, V)
plt.plot(Volume_df["VMA"])
plt.show()

Сообщение об ошибке:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-33-923bfbd060a7> in <module>
     50 
     51 for x in range(len(Volume_df)):
---> 52     if(Volume_df["Valid"][x]<0):
     53         if(X[x]>O[x]):
     54             Scatter_Volume = Scatter_Volume.append({"X":int(x),

A:\Anaconda\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    866         key = com.apply_if_callable(key, self)
    867         try:
--> 868             result = self.index.get_value(self, key)
    869 
    870             if not is_scalar(result):

A:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   4373         try:
   4374             return self._engine.get_value(s, k,
-> 4375                                           tz=getattr(series.dtype, 'tz', None))
   4376         except KeyError as e1:
   4377             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

KeyError: 0
...