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

I have the above panel where i have created Items (Columns) Range, ATR1, and ATR2

i would like to create another item (Column) ... TR and was referred to these 2 options in a similar post

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

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

i have tried both but get the error

---------------------------------------------------------------------------
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'

i posted this question originally failing to realize that this was a panel and not a dataframe. the two lines of code are the solution for a dataframe but do not work here.

wlbsr
  • 13
  • 2

1 Answers1

0

Check out the pandas documentation on descriptive statistics: http://pandas.pydata.org/pandas-docs/stable/basics.html#descriptive-statistics

Just do

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]
Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
  • 1
    I think the OP wants the maximum of the rows, not the columns. The wording is ambiguous but the example output seems to be row-based. – DSM Feb 25 '14 at 01:37
  • Yep, that's right. I was just giving example usage and hoped the OP would figure out that the axis parameter controls that. – Phillip Cloud Feb 25 '14 at 06:51