Python iterrows,ix,loc错误消息

时间:2017-06-18 13:38:21

标签: python pandas

我试图用我这样的基本逻辑来填充我的交易网格: 如果单元格值大于0,则如果它等于0则不执行任何操作,将最后一个值分配给它,最后。如果当前单元格值为负数,则从前一个值中减去当前值。这是代码和错误消息:

df_trades.head(10)
               GOOG  AAPL  XOM   IBM
2011-01-10     0  1500    0     0
2011-01-11     0     0    0     0
2011-01-12     0     0    0     0
2011-01-13     0 -1500    0  4000
2011-01-14     0     0    0     0
2011-01-18     0     0    0     0
2011-01-19     0     0    0     0
2011-01-20     0     0    0     0
2011-01-21     0     0    0     0
2011-01-24     0     0    0     0
>>> df_cols = df_trades.columns
>>> for index,row in df_trades.iterrows():
        index_loc = df_trades.index.get_loc(index)
        if(index_loc !=0):
                for i in df_cols:
                        if df_trades.ix[index,row[i]] == 0.0:
                                df_trades.loc[index,row[i]] = df_trades.ix[index_loc - 1,row[i]]
                        elif df_trades.ix[index,row[i]] < 0.0:
                                df_trades.loc[index,row[i]] = df_trades.ix[index_loc - 1,row[i]] + df_trades.ix[index,row[i]]



Traceback (most recent call last):
  File "<pyshell#245>", line 6, in <module>
    df_trades.loc[index,row[i]] = df_trades.ix[index_loc - 1, row[i]]
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 83, in __setitem__
indexer = self._convert_tuple(key)
   File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 95, in _convert_tuple
idx = self._convert_to_indexer(k, axis=i)
  File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 587, in _convert_to_indexer
return labels.get_loc(obj)
  File "C:\Python27\lib\site-packages\pandas\core\index.py", line 755, in get_loc
return self._engine.get_loc(key)
  File "index.pyx", line 130, in pandas.index.IndexEngine.get_loc (pandas\index.c:3238)
  File "index.pyx", line 150, in pandas.index.IndexEngine.get_loc (pandas\index.c:3118)
  File "hashtable.pyx", line 670, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:10498)
  File "hashtable.pyx", line 676, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:10452)

KeyError:0.0

0 个答案:

没有答案