简体   繁体   English

如何标记 DataFrame 列中 PREVIOUS 三个值相同的行?

[英]How to flag rows where PREVIOUS three values are same in a DataFrame column?

My dataframe has the following format:我的数据框具有以下格式:

final_df=pd.DataFrame([{'Open':100,'Close':101,'Candle':1},{'Open':100,'Close':101,'Candle':1},{'Open':101,'Close':102,'Candle':1},{'Open':102,'Close':101,'Candle':0},{'Open':101,'Close':100,'Candle':0},{'Open':100,'Close':99,'Candle':0},{'Open':99,'Close':98,'Candle':0},{'Open':98,'Close':99,'Candle':1},{'Open':99,'Close':100,'Candle':1},{'Open':100,'Close':101,'Candle':1},{'Open':100,'Close':99,'Candle':0}])

I would like to create a column called pattern which has the value 1 every time three or more values of the column 'candle' are the same.我想创建一个名为pattern的列,每次“蜡烛”列的三个或更多值相同时,其值为1 I tried using the following code but this code flags entries in which 3 (or more) consequent values are the same, instead I want to flag the start of this pattern, ie.我尝试使用以下代码,但此代码标记其中 3 个(或更多)结果值相同的条目,而不是我想标记此模式的开始,即。 have 1 for the row with the third, fourth, ... candle.1代表第三、第四、……蜡烛的行。

final_df['pattern'] = final_df.Candle.groupby([final_df.Candle.diff().ne(0).cumsum()]).transform('size').ge(3).astype(int) 

In this example, I want the rows with the index 2, 5, 6, and 9 to be flagged as these are the data points where the previous three rows have the same value.在此示例中,我希望标记索引为 2、5、6 和 9 的行,因为这些是前三行具有相同值的数据点。

You can try rolling the window and count the values in window are same by checking the Series.value_counts length您可以尝试滚动窗口并通过检查Series.value_counts长度来计算窗口中的值是否相同

final_df['pattern'] = (final_df['Candle'].rolling(3).apply(lambda s: (len(s.value_counts()) == 1))
                       .fillna(0)
                       .astype(int))
print(final_df)

    Open  Close  Candle  pattern
0    100    101       1        0
1    100    101       1        0
2    101    102       1        1
3    102    101       0        0
4    101    100       0        0
5    100     99       0        1
6     99     98       0        1
7     98     99       1        0
8     99    100       1        0
9    100    101       1        1
10   100     99       0        0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 总结 dataframe:如果键的所有行都具有相同的“类型”,则键列(唯一值)加上标志 - Summarize dataframe: key column (unique values) plus flag if all rows for the key have the same 'type' 创建新的 dataframe 列并根据同一列的先前行值生成值 - Create new dataframe column and generate values depending on the previous rows value of this same column 如何提取指定列值组合重复的数据帧的行? - How to extract the rows of a dataframe where a combination of specified column values are duplicated? 如何删除列的值在集合中的DataFrame行? - How to remove DataFrame rows where a column's values are in a set? 如何标记列值集匹配的DataFrame行? - How to label rows of DataFrame where the set of column values match? 从 pandas dataframe 的列中获取上一个和下一个三个值 - Get the previous and next three values from an column of pandas dataframe 如何计算 dataframe 中同一列中特定值之后的行数 - how to count the number of rows following a particular values in the same column in a dataframe 如果对前面的行有感知变化,则替换值 dataframe 列值 - Replace values dataframe column values if there is a perceptual change regarding the previous rows 如何将带有值列表的列转换为 Pandas DataFrame 中的行,也包括前一列 - How to convert column with list of values into rows in Pandas DataFrame including previous column also 在Pandas数据框中查找具有相同列值的行 - Finding rows with same column values in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM