Python 是否在数据帧中查找连续出现的字符串?

Python 是否在数据帧中查找连续出现的字符串?,python,pandas,dataframe,Python,Pandas,Dataframe,我有这样一个数据帧: log_alerts 0 no 1 yes 2 yes 3 no 4 yes 5 yes 6 yes 7 yes 我试图在log_alerts(日志警报)列中找到持续出现的yes(是),在第三次出现时,代码应该会提示 预期产出: log_alerts message 0 no none 1

我有这样一个数据帧:

  log_alerts 
0         no   
1        yes    
2        yes   
3         no    
4        yes   
5        yes
6        yes
7        yes
我试图在log_alerts(日志警报)列中找到持续出现的yes(是),在第三次出现时,代码应该会提示

预期产出:

  log_alerts    message
0         no   none
1        yes   none
2        yes   none
3         no   none
4        yes   none
5        yes   none
6        yes   continuity found
7        yes   Review again
我怎样才能做到这一点

可以使用熊猫图书馆完成吗?

我相信您需要:

  • 首先通过比较ed列和
  • 仅筛选
    yes
  • 通过获取每个组的计数,并添加与原始数据帧具有相同索引的
    系列
  • 上次创建新列的方法


详细信息

print (type(a))
<class 'pandas.core.series.Series'>

print (a)
1    2
2    2
4    4
5    4
6    4
7    4
Name: log_alerts, dtype: int32
打印(类型(a))
印刷品(a)
1    2
2    2
4    4
5    4
6    4
7    4
名称:日志警报,数据类型:int32

如果
7 yes
,会发生什么情况?@jezrael,那么它也应该给出与“review”相同的提示。@jezrael,最后一行的输入如中所示??是,现在它是正确的。它给出了一个属性错误,即“AttributeError:无法访问'DataFrameGroupBy'对象的属性'index',请尝试使用'apply'方法”我使用了cumcount。@Dheeraj-
详细信息
已添加到答案中。
masks = [counts == 2, counts > 2]
df['message'] = np.select(masks, ['continuity found','Review again'], default=None)
print (df)
  log_alerts           message
0         no              None
1        yes              None
2        yes              None
3         no              None
4        yes              None
5        yes              None
6        yes  continuity found
7        yes      Review again
print (type(a))
<class 'pandas.core.series.Series'>

print (a)
1    2
2    2
4    4
5    4
6    4
7    4
Name: log_alerts, dtype: int32