2014-02-25 60 views
0
In [63]: 

yahoo['Range'] = yahoo['High']-yahoo['Low'] 
yahoo['ATR1'] = abs(yahoo['Prev Close']-yahoo['High']) 
yahoo['ATR2'] = abs(yahoo['Prev Close']-yahoo['Low']) 

yahoo 

<class 'pandas.core.panel.Panel'> 
Dimensions: 34 (items) x 804 (major_axis) x 14 (minor_axis) 
Items axis: Open to Gap 
Major_axis axis: 2010-12-13 00:00:00 to 2014-02-24 00:00:00 
Minor_axis axis: AA to XOM 

我已經在那裏我已經創建項目(列)範圍,ATR1和ATR2基本的數學函數max()幫助:在面板

我想創建上述面板其他項目(列)... TR,並提到這兩個選項中類似的職位

yahoo['TR'] = yahoo[['Range', 'ATR1', 'ATR2']].max(axis=1) 

#yahoo['TR'] = yahoo[['Range', 'ATR1', 'ATR2']].apply(max, axis=1) 

我曾經嘗試都,但得到的錯誤

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-64-15309b584b96> in <module>() 
----> 1 yahoo['TR'] = yahoo[['Range', 'ATR1', 'ATR2']].max(axis=1) 
     2 
     3 #yahoo['TR'] = yahoo[['Range', 'ATR1', 'ATR2']].apply(max, axis=1) 
     4 

C:\Anaconda\lib\site-packages\pandas\core\panel.pyc in __getitem__(self, key) 
    250   if isinstance(self._info_axis, MultiIndex): 
    251    return self._getitem_multilevel(key) 
--> 252   return super(Panel, self).__getitem__(key) 
    253 
    254  def _getitem_multilevel(self, key): 

C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in __getitem__(self, item) 
    975 
    976  def __getitem__(self, item): 
--> 977   return self._get_item_cache(item) 
    978 
    979  def _get_item_cache(self, item): 

C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in _get_item_cache(self, item) 
    979  def _get_item_cache(self, item): 
    980   cache = self._item_cache 
--> 981   res = cache.get(item) 
    982   if res is None: 
    983    values = self._data.get(item) 

TypeError: unhashable type: 'list' 

我發佈了這個問題最初沒有意識到這是一個面板而不是數據框。這兩行代碼是數據框的解決方案,但在這裏不起作用。

+2

基本上data.max(軸= 1) – YXD

回答

0

檢查出的描述性統計大熊貓文檔:http://pandas.pydata.org/pandas-docs/stable/basics.html#descriptive-statistics

只是做

In [29]: df = DataFrame(randn(3, 3)) 

In [30]: df 
Out[30]: 
      0   1   2 
0 0.745811 1.703260 -0.114727 
1 0.445875 1.692970 -0.320290 
2 -0.162003 -1.341548 -1.665276 

[3 rows x 3 columns] 

In [31]: df['a'] = df.max() 

In [32]: df 
Out[32]: 
      0   1   2   a 
0 0.745811 1.703260 -0.114727 0.745811 
1 0.445875 1.692970 -0.320290 1.703260 
2 -0.162003 -1.341548 -1.665276 -0.114727 

[3 rows x 4 columns] 
+1

我認爲OP希望最大的行,而不是列。措辭不明確,但示例輸出似乎是基於行的。 – DSM

+0

是的,沒錯。我只是舉例說明使用情況,並希望OP會發現軸參數控制着這一點。 –