Pandas行间计算

时间:2013-01-24 02:25:00

标签: pandas

我有一个包含每日OHLCV数据的DataFrame。

我可以用以下方式计算范围:

s['Range'] = s['High'] - s['Low']

简单。现在我想计算一个我称之为s['OIR']的新列(OIR = Open-In-Range)

['OIR']列检查我们是否在范围内开盘,并通过测试我们是否在昨天的低点和低于昨天的高点开盘来测试。我需要引用前面的行,我不太清楚如何做到这一点。返回值为True / False。

感谢。


编辑:我是StackExchange和Python的新手。不确定从哪里删除样本数据。这是数据帧的图像。

http://i47.tinypic.com/142eb2a.png


示例数据:字典转换为DataFrame

{'High': {<Timestamp: 2007-03-02 00:00:00>: 1384.5,
  <Timestamp: 2007-03-05 00:00:00>: 1373.0},
 'Last': {<Timestamp: 2007-03-02 00:00:00>: 1365.0,
  <Timestamp: 2007-03-05 00:00:00>: 1351.5},
 'Low': {<Timestamp: 2007-03-02 00:00:00>: 1364.25,
  <Timestamp: 2007-03-05 00:00:00>: 1350.5},
 'OIR': {<Timestamp: 2007-03-02 00:00:00>: False,
  <Timestamp: 2007-03-05 00:00:00>: False},
 'Open': {<Timestamp: 2007-03-02 00:00:00>: 1378.5,
  <Timestamp: 2007-03-05 00:00:00>: 1356.75},
 'Range': {<Timestamp: 2007-03-02 00:00:00>: 20.25,
 <Timestamp: 2007-03-05 00:00:00>: 22.5},
 'Volume': {<Timestamp: 2007-03-02 00:00:00>: 1706906,
 <Timestamp: 2007-03-05 00:00:00>: 1984041}}

答案:

s['OIR'] = ((s['Open'] < s['High'].shift(1)) & (s['Open'] > s['Low'].shift(1)))

1 个答案:

答案 0 :(得分:7)

以您建议的方式引用以前的行最好使用Series.shift()函数完成:

In [1]: df = DataFrame(randn(10,3),columns=['O','L','H'])

In [2]: df
Out[2]:
          O         L         H
0  0.605412  0.739866 -0.280222
1 -0.707852  0.785651  0.855183
2 -0.087119  0.518924  0.932167
3 -0.913352  0.369825  1.277771
4  0.434593 -2.942903  0.802413
5  0.075669 -0.135914  1.374454
6  1.112062  0.314946  0.882468
7 -0.706078 -0.202243  0.838088
8 -1.668152  0.414585  0.809932
9  1.452937 -0.048245  0.635499

In [3]: df['OIR'] = ((df.L.shift() <= df.O) & (df.O <= df.H.shift()))

In [4]: df
Out[4]:
          O         L         H    OIR
0  0.605412  0.739866 -0.280222  False
1 -0.707852  0.785651  0.855183  False
2 -0.087119  0.518924  0.932167  False
3 -0.913352  0.369825  1.277771  False
4  0.434593 -2.942903  0.802413   True
5  0.075669 -0.135914  1.374454   True
6  1.112062  0.314946  0.882468   True
7 -0.706078 -0.202243  0.838088  False
8 -1.668152  0.414585  0.809932  False
9  1.452937 -0.048245  0.635499  False