前几行中连续出现的 pandas 数 [英] Pandas number of consecutive occurrences in previous rows

查看:95
本文介绍了前几行中连续出现的 pandas 数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有OHLC数据.蜡烛可以是绿色"(如果收盘价高于收盘价)或红色"(如果收盘价高于收盘价).格式为:

I have OHLC data. The candle can be either 'green' (if the close is above open) or 'red' (if the open is above the close). The format is:

  open close candletype
0  542 543 GREEN
1  543 544 GREEN 
2  544 545 GREEN
3  545 546 GREEN
4  546 547 GREEN
5  547 542 RED
6  542 543 GREEN

我想计算n个先前行中连续的绿色或红色蜡烛的数量.可以说我要确定在3个绿色蜡烛前面的行.

What I would like is to count the number of consecutive green or red candles for n-previous rows. Lets say I want to identify rows preceded by 3 green candles.

所需的输出将是:

  open close candletype  pattern
0  542 543 GREEN  Toofewrows
1  543 544 GREEN  Toofewrows
2  544 545 GREEN  Toofewrows
3  545 546 GREEN  3-GREEN-CANDLES-IN-A-ROW
4  546 547 GREEN  3-GREEN-CANDLES-IN-A-ROW
5  547 542 RED    3-GREEN-CANDLES-IN-A-ROW
6  542 543 GREEN  No pattern

我知道如何通过提取行号,将自定义函数应用于具有该行号的Candletype系列并查看该自定义函数中的前n行,创建n项目列表并检查isAll(')来获取解决方案.绿色"),但如果有一个高级的衬套解决方案,我会很想知道吗?

I know how to get the solution by extracting the row number, applying a custom function to candletype series with that row number and looking at n previous rows within that custom function, creating a n-item list and checking for isAll('GREEN') but I WAS WONDERING IF THERE IS AN ELEGANT ONE LINER APPLY SOLUTION?

推荐答案

您可以将lambda函数应用于滚动窗口.参见将lambda函数应用于熊猫滚动窗口系列

You can apply lambda functions to rolling windows. See Applying lambda function to a pandas rolling window series

您可以对它们进行分类,也可以将它们自己映射为一些数字:

You can either categorize them or map them on your own to some numbers:

df = pd.read_clipboard()
df['code'] = df.candletype.astype('category').cat.codes

这将导致以下DataFrame:

This results in following DataFrame:

    open    close   candletype  code
0   542 543 GREEN   0
1   543 544 GREEN   0
2   544 545 GREEN   0
3   545 546 GREEN   0
4   546 547 GREEN   0
5   547 542 RED 1
6   542 543 GREEN   0

现在只需应用df['code'].rolling(3).apply(lambda x: all(x==0)).shift(),结果 0

Now just apply df['code'].rolling(3).apply(lambda x: all(x==0)).shift(), resulting in 0

     NaN
1    NaN
2    NaN
3    1.0
4    1.0
5    1.0
6    0.0

并按期望的/期望的方式填充您的nans和零.

and fill your nans and zeros as expected/wanted.

这既不是一个单行,但可能比字符串比较漂亮.希望对您有帮助!

This neither is a oneliner, but maybe more pretty than the string comparison. Hope it helps you!

这篇关于前几行中连续出现的 pandas 数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆