尝试检查我的数据框列中的值是否低于0时收到KeyError:0

时间:2019-10-08 21:19:51

标签: python pandas dataframe keyerror

我对编码非常陌生,并且一直在使用此脚本。每当我尝试运行它时,for循环后的第一个if语句都会出错。

不确定是什么错误,由于存在零,它表示是KeyError。

我要做的是检查数据框的“有效”列的值是否小于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

0 个答案:

没有答案