如何使用多个groupby列从OHLC数据计算枢轴值

时间:2019-05-20 13:58:04

标签: python-3.x pandas pivot finance technical-indicator

我有一个带有open,high,low,close,key1和key2列的熊猫数据集。现在,我想按key1和key2对数据集进行分组,并使用公式-(高+低+闭合)/ 3计算枢轴。到目前为止,我已经能够做到。但是要求是将计算出的数据移到我无法编码的下一组。

我能够按key1和key2列对数据集进行分组,并能够通过以下代码计算数据透视表,但是无法将值移至下一组。

import pandas as pd

data = pd.DataFrame([[110, 115, 105, 111, 1, 2],[11, 16, 6, 12, 1, 2],[12, 17, 7, 13, 1, 3],[22, 25, 17, 20, 1, 3],[12, 16, 6, 11, 2, 4],[32, 36, 26, 28, 2, 4],[9, 13, 4, 13, 2, 5],[49, 53, 40, 45, 2, 5],[13, 18, 9, 12, 3, 6],[14, 16, 10, 13, 3, 6]], columns=["open","high","low","close","key1", "key2"])
s = (data.high.groupby([data.key1, data.key2]).max() + data.low.groupby([data.key1, data.key2]).min() + data.close.groupby([data.key1, data.key2]).last()) / 3
#data['pivot'] = data['key1', 'key2'].map(s.shift())
print(data)

当我使用以下代码时,

import pandas as pd

data = pd.DataFrame([[110, 115, 105, 111, 1, 2],[11, 16, 6, 12, 1, 2],[12, 17, 7, 13, 1, 3],[22, 25, 17, 20, 1, 3],[12, 16, 6, 11, 2, 4],[32, 36, 26, 28, 2, 4],[9, 13, 4, 13, 2, 5],[49, 53, 40, 45, 2, 5],[13, 18, 9, 12, 3, 6],[14, 16, 10, 13, 3, 6]], columns=["open","high","low","close","key1", "key2"])
data['pivot'] = (data.high.groupby([data.key1, data.key2]).transform('max') + data.low.groupby([data.key1, data.key2]).transform('min') + data.close.groupby([data.key1, data.key2]).transform('last')) / 3
print(data)

我得到了低于输出的结果。

   open  high  low  close  key1  key2      pivot
0   110   115  105    111     1     2  44.333333
1    11    16    6     12     1     2  44.333333
2    12    17    7     13     1     3  17.333333
3    22    25   17     20     1     3  17.333333
4    12    16    6     11     2     4  23.333333
5    32    36   26     28     2     4  23.333333
6     9    13    4     13     2     5  34.000000
7    49    53   40     45     2     5  34.000000
8    13    18    9     12     3     6  13.333333
9    14    16   10     13     3     6  13.333333

但预期输出:

   open  high  low  close  key1  key2     pivot
0   110   115  105    111     1     2      NaN
1    11    16    6     12     1     2      NaN
2    12    17    7     13     1     3   44.333333
3    22    25   17     20     1     3   44.333333
4    12    16    6     11     2     4   17.333333
5    32    36   26     28     2     4   17.333333
6     9    13    4     13     2     5   23.333333
7    49    53   40     45     2     5   23.333333
8    13    18    9     12     3     6   34.000000
9    14    16   10     13     3     6   34.000000

1 个答案:

答案 0 :(得分:0)

首先对字典和GroupBy.agg使用聚合函数,然后对shift使用新列DataFrame.join

s = data.groupby(['key1','key2']).agg({'low':'min','high':'max','close':'last'}).sum(axis=1)/3

data = data.join(s.rename('pivot').shift(), on=['key1','key2'])
print (data)
   open  high  low  close  key1  key2      pivot
0   110   115  105    111     1     2        NaN
1    11    16    6     12     1     2        NaN
2    12    17    7     13     1     3  44.333333
3    22    25   17     20     1     3  44.333333
4    12    16    6     11     2     4  17.333333
5    32    36   26     28     2     4  17.333333
6     9    13    4     13     2     5  23.333333
7    49    53   40     45     2     5  23.333333
8    13    18    9     12     3     6  34.000000
9    14    16   10     13     3     6  34.000000