基本数学函数max()帮助pandas或numpy redux:用于面板

时间:2014-02-25 00:22:20

标签: pandas

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

我想创建另一个项目(列)... 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'

我发布这个问题最初没有意识到这是一个小组而不是数据帧。这两行代码是数据帧的解决方案,但在这里不起作用。

1 个答案:

答案 0 :(得分:0)

查看有关描述性统计信息的pandas文档: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]